Skip to content

Commit

Permalink
Fixed a bug caused when the SchemaGenerationFeature.INLINE_DEFINITION…
Browse files Browse the repository at this point in the history
…S 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)
  • Loading branch information
david-waltermire authored Sep 18, 2023
1 parent 9afd340 commit 5e02200
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<SchemaGenerationFeature<?>> 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<Element> xpath = XPathFactory.instance()
.compile("//xs:attribute[not(@type or xs:simpleType)]",
Filters.element(),
null,
Namespace.getNamespace("xs", "http://www.w3.org/2001/XMLSchema"));
List<Element> result = xpath.evaluate(document);

assertTrue(result.isEmpty());
}
}
}

0 comments on commit 5e02200

Please sign in to comment.