Fix XML Namespace Handling in serializeExceptionToXml 01/109801/1
authorYaroslav Lastivka <yaroslav.lastivka@pantheon.tech>
Tue, 16 Jan 2024 13:15:21 +0000 (15:15 +0200)
committerYaroslav Lastivka <yaroslav.lastivka@pantheon.tech>
Tue, 16 Jan 2024 13:35:49 +0000 (15:35 +0200)
In the commit 1b59089de6b8306b3f6c9dd7b14299c14f6c00d3,
we inadvertently introduced an issue in the
serializeExceptionToXml method where the XML namespace was
set in a manner not compliant with XML standards,
causing issues in XML parsing.
Additionally, the method did not utilize the currentDatabindContext
for writing the error-path, which could lead to incorrect
serialization of error paths in certain contexts.

The fix involves correctly setting the default XML namespace for
the errors element. Additionally, the method now properly utilizes
the currentDatabindContext for encoding error paths.

JIRA: NETCONF-1130
Change-Id: Ia7900bff2e63d23213896b5e0c96a514ace92873
Signed-off-by: Yaroslav Lastivka <yaroslav.lastivka@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/RestconfDocumentedExceptionMapper.java

index 276ad51b2b1df5f18e0df00f8ca316e13b441e7b..73e5962ae2462ad72ff9b14caa092f634a6b3262 100644 (file)
@@ -189,9 +189,11 @@ public final class RestconfDocumentedExceptionMapper implements ExceptionMapper<
         try (var outputStream = new ByteArrayOutputStream()) {
             final var xmlWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(outputStream,
                 StandardCharsets.UTF_8.name());
+            final var currentDatabindContext = exception.modelContext() != null
+                ? DatabindContext.ofModel(exception.modelContext()) : databindProvider.currentDatabind();
             xmlWriter.writeStartDocument();
             xmlWriter.writeStartElement(Errors.QNAME.getLocalName());
-            xmlWriter.writeNamespace("xmlns", Errors.QNAME.getNamespace().toString());
+            xmlWriter.writeDefaultNamespace(Errors.QNAME.getNamespace().toString());
             if (exception.getErrors() != null && !exception.getErrors().isEmpty()) {
                 for (final var error : exception.getErrors()) {
                     xmlWriter.writeStartElement(Error.QNAME.getLocalName());
@@ -202,7 +204,7 @@ public final class RestconfDocumentedExceptionMapper implements ExceptionMapper<
 
                     if (error.getErrorPath() != null) {
                         xmlWriter.writeStartElement(ERROR_PATH_QNAME.getLocalName());
-                        databindProvider.currentDatabind().xmlCodecs().instanceIdentifierCodec()
+                        currentDatabindContext.xmlCodecs().instanceIdentifierCodec()
                             .writeValue(xmlWriter, error.getErrorPath());
                         xmlWriter.writeEndElement();
                     }