From 5e02200d07e83b37fb2c178b87f984934e2e214a Mon Sep 17 00:00:00 2001 From: David Waltermire Date: Mon, 18 Sep 2023 13:29:02 -0400 Subject: [PATCH] Fixed a bug caused when the SchemaGenerationFeature.INLINE_DEFINITIONS is enabled causing XML schema type references to not be produced for attributes associated with flags. This change restores the correct behavior by adding a type.isGeneratedType conditional check. Resolves usnistgov/liboscal-java#181. (#218) --- .../schematype/AbstractXmlComplexType.java | 2 +- .../metaschema/schemagen/XmlSuiteTest.java | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/metaschema-schema-generator/src/main/java/gov/nist/secauto/metaschema/schemagen/xml/schematype/AbstractXmlComplexType.java b/metaschema-schema-generator/src/main/java/gov/nist/secauto/metaschema/schemagen/xml/schematype/AbstractXmlComplexType.java index 86ef09f27..3a173603d 100644 --- a/metaschema-schema-generator/src/main/java/gov/nist/secauto/metaschema/schemagen/xml/schematype/AbstractXmlComplexType.java +++ b/metaschema-schema-generator/src/main/java/gov/nist/secauto/metaschema/schemagen/xml/schematype/AbstractXmlComplexType.java @@ -93,7 +93,7 @@ protected static void generateFlagInstance(@NonNull IFlagInstance instance, @Non IXmlType type = state.getTypeForDefinition(definition); - if (state.isInline(definition)) { + if (state.isInline(definition) && type.isGeneratedType(state)) { DocumentationGenerator.generateDocumentation(instance, state); type.generateType(state, true); diff --git a/metaschema-schema-generator/src/test/java/gov/nist/secauto/metaschema/schemagen/XmlSuiteTest.java b/metaschema-schema-generator/src/test/java/gov/nist/secauto/metaschema/schemagen/XmlSuiteTest.java index aaed97ff9..8af71c772 100644 --- a/metaschema-schema-generator/src/test/java/gov/nist/secauto/metaschema/schemagen/XmlSuiteTest.java +++ b/metaschema-schema-generator/src/test/java/gov/nist/secauto/metaschema/schemagen/XmlSuiteTest.java @@ -26,6 +26,8 @@ package gov.nist.secauto.metaschema.schemagen; +import static org.junit.jupiter.api.Assertions.assertTrue; + import gov.nist.secauto.metaschema.binding.io.Format; import gov.nist.secauto.metaschema.model.MetaschemaLoader; import gov.nist.secauto.metaschema.model.common.IMetaschema; @@ -36,6 +38,14 @@ import gov.nist.secauto.metaschema.model.common.validation.XmlSchemaContentValidator; import gov.nist.secauto.metaschema.schemagen.xml.XmlSchemaGenerator; +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.Namespace; +import org.jdom2.filter.Filters; +import org.jdom2.input.StAXEventBuilder; +import org.jdom2.xpath.XPathExpression; +import org.jdom2.xpath.XPathFactory; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DynamicNode; @@ -44,17 +54,24 @@ import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; +import java.io.FileReader; import java.io.IOException; +import java.io.Reader; import java.io.Writer; import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Stream; +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; + class XmlSuiteTest extends AbstractSchemaGeneratorTestSuite { // private static final XmlSchemaContentValidator SCHEMA_VALIDATOR; @@ -184,4 +201,40 @@ void testOSCALComplete() throws IOException, MetaschemaException { // NOPMD - de schemaGenerator.generateFromMetaschema(metaschema, writer, features); } } + + @Test + void testLiboscalJavaIssue181() throws IOException, MetaschemaException, XMLStreamException, JDOMException { + MetaschemaLoader loader = new MetaschemaLoader(); + loader.allowEntityResolution(); + + IMetaschema module = loader.load(new URL( + "https://raw.githubusercontent.com/usnistgov/OSCAL/v1.1.1/src/metaschema/oscal_catalog_metaschema.xml")); + ISchemaGenerator schemaGenerator = new XmlSchemaGenerator(); + IMutableConfiguration> features = new DefaultConfiguration<>(); + features.enableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS); + features.disableFeature(SchemaGenerationFeature.INLINE_CHOICE_DEFINITIONS); + + Path schemaPath = Path.of("target/oscal-catalog_schema.xsd"); + try (Writer writer = Files.newBufferedWriter(schemaPath, StandardCharsets.UTF_8, getWriteOpenOptions())) { + assert writer != null; + schemaGenerator.generateFromMetaschema(module, writer, features); + } + + // check for missing attribute types per liboscal-java#181 + XMLInputFactory factory = XMLInputFactory.newFactory(); + try (Reader fileReader = new FileReader(schemaPath.toFile())) { + XMLEventReader reader = factory.createXMLEventReader(fileReader); + StAXEventBuilder builder = new StAXEventBuilder(); + Document document = document = builder.build(reader); + + XPathExpression xpath = XPathFactory.instance() + .compile("//xs:attribute[not(@type or xs:simpleType)]", + Filters.element(), + null, + Namespace.getNamespace("xs", "http://www.w3.org/2001/XMLSchema")); + List result = xpath.evaluate(document); + + assertTrue(result.isEmpty()); + } + } }