Skip to content

Support xs:enumeration restrictions in XSD generation via field metadata - #634

Merged
gnodet merged 2 commits into
codehaus-plexus:masterfrom
gnodet:fix/xsd-enumeration-metadata
Nov 10, 2026
Merged

gnodet merged 2 commits into
codehaus-plexus:masterfrom
gnodet:fix/xsd-enumeration-metadata

Conversation

@gnodet

@gnodet gnodet commented Nov 9, 2026

Copy link
Copy Markdown
Member

Fixes #633

Summary

Adds three new field-level XSD metadata attributes to modello-plugin-xsd:

Attribute Mode Generated XSD
xsd.enumerations Strict — rejects unknown values xs:restriction with xs:enumeration
xsd.suggestions Non-strict — IDE completion only xs:union of enumeration + xs:string
xsd.pattern Regex validation xs:restriction with xs:pattern

Usage

<!-- Strict: only compile/runtime/provided/test/system are schema-valid -->
<field xsd.enumerations="compile,runtime,provided,test,system">
  <name>scope</name>
  <type>String</type>
</field>

<!-- Suggested: IDE completion for known values, custom values still accepted -->
<field xsd.suggestions="jar,pom,war,ear,maven-plugin">
  <name>packaging</name>
  <type>String</type>
</field>

<!-- Pattern: validates coordinate format -->
<field xsd.pattern="[a-zA-Z0-9._\-]+">
  <name>artifactId</name>
  <type>String</type>
</field>

Generated XSD (strict enumeration example)

<xs:element minOccurs="0" name="scope">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="compile"/>
      <xs:enumeration value="runtime"/>
      <xs:enumeration value="provided"/>
      <xs:enumeration value="test"/>
      <xs:enumeration value="system"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Changes

  • XsdFieldMetadata: add enumerations, suggestions, pattern fields
  • XsdMetadataPlugin: parse xsd.enumerations, xsd.suggestions, xsd.pattern from the field data map
  • XsdGenerator: emit inline xs:simpleType when any constraint is present; plain type="xs:string" used only for unconstrained fields; also supports constrained xs:attribute fields
  • EnumerationXsdGeneratorTest + fixtures: cover all three modes, verify structural XSD content via XPath, validate that good XML passes and bad values/patterns are rejected, and confirm that custom values outside xsd.suggestions still pass validation

Add three new field-level XSD metadata attributes:
- xsd.enumerations: strict restriction — rejects unknown values
- xsd.suggestions: non-strict union — IDE autocompletion without rejection
- xsd.pattern: pattern restriction via xs:pattern facet

Changes:
- XsdFieldMetadata: add enumerations, suggestions, pattern fields
- XsdMetadataPlugin: parse xsd.enumerations, xsd.suggestions, xsd.pattern
  from the field data map (comma-separated for enum/suggestion lists)
- XsdGenerator: emit inline xs:simpleType with xs:restriction/xs:enumeration,
  xs:union, or xs:pattern when the metadata is present; plain xs:string
  type attribute is used only when no constraint is defined
- EnumerationXsdGeneratorTest + fixtures: cover all three modes plus the
  non-strict round-trip (custom packaging value must pass validation)

Fixes codehaus-plexus#633

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR adds xsd.enumerations, xsd.suggestions, and xsd.pattern field metadata to the Modello XSD generator — a genuinely useful feature with thorough Javadoc and XSD validation tests. A few things to address before this is ready:

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following up on my earlier review — all 4 findings have been addressed:

  • ✅ getEnumerations() / getSuggestions() — now return Collections.unmodifiableList(...), preventing external mutation.
  • ✅ Mutual-exclusivity validation — getFieldMetadata() now throws ModelloException when more than one of xsd.enumerations, xsd.suggestions, or xsd.pattern is set on the same field.
  • ✅ xml.attribute="true" test coverage — attributeScope field added to the .mdo fixture with enumeration constraint; assertion 5 in testEnumerationConstraints verifies the attribute-field code path.

LGTM.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet gnodet added the enhancement New feature or request label Nov 10, 2026
@gnodet
gnodet merged commit 9e7f1c6 into codehaus-plexus:master Nov 10, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support xs:enumeration restrictions in XSD generation via field metadata

2 participants