From d03083aea087dc62cf3af09911fd3c4f6c264383 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 10 Nov 2022 17:57:52 +0100 Subject: [PATCH] Do not use SchemaPath for error-info writeout We can use simple SchemaInferenceStack to acquire the state for being in a grouping, thus eliminating this particular vestige. Change-Id: I6e2642b56b17d013afa81500aac8e633e4f6d253 Signed-off-by: Robert Varga --- .../JsonStreamWriterWithDisabledValidation.java | 14 +++++++------- .../errors/RestconfDocumentedExceptionMapper.java | 7 ++----- .../errors/StreamWriterWithDisabledValidation.java | 10 ++++++++++ .../XmlStreamWriterWithDisabledValidation.java | 12 ++++++------ 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/JsonStreamWriterWithDisabledValidation.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/JsonStreamWriterWithDisabledValidation.java index 84c8718879..d84415d11b 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/JsonStreamWriterWithDisabledValidation.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/JsonStreamWriterWithDisabledValidation.java @@ -19,7 +19,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStre import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier; import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; /** * JSON stream-writer with disabled leaf-type validation for specified QName. @@ -41,13 +40,14 @@ final class JsonStreamWriterWithDisabledValidation extends StreamWriterWithDisab * @param schemaContextHandler Handler that holds actual schema context. */ JsonStreamWriterWithDisabledValidation(final QName excludedQName, final OutputStreamWriter outputWriter, - final SchemaPath schemaPath, final XMLNamespace initialNs, - final SchemaContextHandler schemaContextHandler) { + final XMLNamespace initialNs, final SchemaContextHandler schemaContextHandler) { super(excludedQName); - this.jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter, DEFAULT_INDENT_SPACES_NUM); - this.jsonNodeStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter( - JSONCodecFactorySupplier.RFC7951.getShared(schemaContextHandler.get()), - schemaPath, initialNs, jsonWriter); + jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter, DEFAULT_INDENT_SPACES_NUM); + + final var inference = errorsContainerInference(schemaContextHandler); + jsonNodeStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter( + JSONCodecFactorySupplier.RFC7951.getShared(inference.getEffectiveModelContext()), + inference, initialNs, jsonWriter); } @Override 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 8b94593e5f..f82a501287 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 @@ -45,7 +45,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter; import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,8 +57,6 @@ public final class RestconfDocumentedExceptionMapper implements ExceptionMapper< private static final Logger LOG = LoggerFactory.getLogger(RestconfDocumentedExceptionMapper.class); private static final MediaType DEFAULT_MEDIA_TYPE = MediaType.APPLICATION_JSON_TYPE; private static final Status DEFAULT_STATUS_CODE = Status.INTERNAL_SERVER_ERROR; - // Note: we are using container's QName reference to trim imports - private static final SchemaPath ERRORS_GROUPING_PATH = SchemaPath.create(true, Errors.QNAME); private static final QName ERROR_TYPE_QNAME = qnameOf("error-type"); private static final QName ERROR_TAG_QNAME = qnameOf("error-tag"); private static final QName ERROR_APP_TAG_QNAME = qnameOf("error-app-tag"); @@ -175,7 +172,7 @@ public final class RestconfDocumentedExceptionMapper implements ExceptionMapper< OutputStreamWriter streamStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); ) { return writeNormalizedNode(errorsContainer, outputStream, new JsonStreamWriterWithDisabledValidation( - ERROR_INFO_QNAME, streamStreamWriter, ERRORS_GROUPING_PATH, IETF_RESTCONF_URI, schemaContextHandler)); + ERROR_INFO_QNAME, streamStreamWriter, IETF_RESTCONF_URI, schemaContextHandler)); } catch (IOException e) { throw new IllegalStateException("Cannot close some of the output JSON writers", e); } @@ -190,7 +187,7 @@ public final class RestconfDocumentedExceptionMapper implements ExceptionMapper< private String serializeErrorsContainerToXml(final ContainerNode errorsContainer) { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { return writeNormalizedNode(errorsContainer, outputStream, new XmlStreamWriterWithDisabledValidation( - ERROR_INFO_QNAME, outputStream, ERRORS_GROUPING_PATH, schemaContextHandler)); + ERROR_INFO_QNAME, outputStream, schemaContextHandler)); } catch (IOException e) { throw new IllegalStateException("Cannot close some of the output XML writers", e); } diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/StreamWriterWithDisabledValidation.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/StreamWriterWithDisabledValidation.java index 611f08c747..8a8579bfa3 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/StreamWriterWithDisabledValidation.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/StreamWriterWithDisabledValidation.java @@ -8,10 +8,14 @@ package org.opendaylight.restconf.nb.rfc8040.jersey.providers.errors; import java.io.IOException; +import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.Errors; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.stream.ForwardingNormalizedNodeStreamWriter; +import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack; +import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference; /** * Created delegating writer to special-case error-info as error-info is defined as an empty container in the restconf @@ -84,4 +88,10 @@ abstract class StreamWriterWithDisabledValidation extends ForwardingNormalizedNo * @throws IOException Writing of the end element to the output stream failed. */ abstract void endNodeWithDisabledValidation() throws IOException; + + static final Inference errorsContainerInference(final SchemaContextHandler schemaContextHandler) { + final var stack = SchemaInferenceStack.of(schemaContextHandler.get()); + stack.enterGrouping(Errors.QNAME); + return stack.toInference(); + } } \ No newline at end of file diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/XmlStreamWriterWithDisabledValidation.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/XmlStreamWriterWithDisabledValidation.java index d1c8917abb..720e5203b3 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/XmlStreamWriterWithDisabledValidation.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/XmlStreamWriterWithDisabledValidation.java @@ -21,13 +21,11 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdent import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; /** * XML stream-writer with disabled leaf-type validation for specified QName. */ final class XmlStreamWriterWithDisabledValidation extends StreamWriterWithDisabledValidation { - private static final XMLOutputFactory XML_FACTORY; static { @@ -47,15 +45,17 @@ final class XmlStreamWriterWithDisabledValidation extends StreamWriterWithDisabl * @param schemaContextHandler Handler that holds actual schema context. */ XmlStreamWriterWithDisabledValidation(final QName excludedQName, final OutputStream outputStream, - final SchemaPath schemaPath, final SchemaContextHandler schemaContextHandler) { + final SchemaContextHandler schemaContextHandler) { super(excludedQName); + try { - this.xmlWriter = XML_FACTORY.createXMLStreamWriter(outputStream, StandardCharsets.UTF_8.name()); + xmlWriter = XML_FACTORY.createXMLStreamWriter(outputStream, StandardCharsets.UTF_8.name()); } catch (final XMLStreamException | FactoryConfigurationError e) { throw new IllegalStateException("Cannot create XML writer", e); } - this.xmlNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, - schemaContextHandler.get(), schemaPath); + + xmlNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, + errorsContainerInference(schemaContextHandler)); } @Override -- 2.36.6