From b9a3d711dd3dc0a6f2bc00121c7fc2da33cfa7c9 Mon Sep 17 00:00:00 2001 From: Yaroslav Lastivka Date: Tue, 16 Jan 2024 15:15:21 +0200 Subject: [PATCH] Fix XML Namespace Handling in serializeExceptionToXml 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 --- .../providers/errors/RestconfDocumentedExceptionMapper.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/RestconfDocumentedExceptionMapper.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/RestconfDocumentedExceptionMapper.java index 276ad51b2b1..73e5962ae24 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/RestconfDocumentedExceptionMapper.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/RestconfDocumentedExceptionMapper.java @@ -189,9 +189,11 @@ private static String serializeExceptionToXml(final RestconfDocumentedException 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 @@ private static String serializeExceptionToXml(final RestconfDocumentedException if (error.getErrorPath() != null) { xmlWriter.writeStartElement(ERROR_PATH_QNAME.getLocalName()); - databindProvider.currentDatabind().xmlCodecs().instanceIdentifierCodec() + currentDatabindContext.xmlCodecs().instanceIdentifierCodec() .writeValue(xmlWriter, error.getErrorPath()); xmlWriter.writeEndElement(); }