Skip to content

XsdGenerator: emit mixed="true" on complexTypes with xml.format="mixed" fields - #628

Merged
gnodet merged 4 commits into
codehaus-plexus:masterfrom
gnodet:fix/xsd-mixed-content-format
Nov 9, 2026
Merged

gnodet merged 4 commits into
codehaus-plexus:masterfrom
gnodet:fix/xsd-mixed-content-format

Conversation

@gnodet

@gnodet gnodet commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

When a Modello field is annotated with xml.format="mixed", the corresponding XML element accepts both free text and child elements. The XSD generator must reflect this with mixed="true" on the xs:complexType.

Two cases are handled:

  1. Field with xml.format="mixed" and xml.tagName (wrapper element): the content lives inside the wrapper child element, so the anonymous xs:complexType written by writeListElement() gets mixed="true".

  2. Field with xml.format="mixed" and no xml.tagName: the content is body text of the class element itself, so the named xs:complexType written by writeComplexTypeDescriptor() gets mixed="true". Fields with xml.tagName are explicitly excluded from this check because their mixed content lives inside a wrapper child element (case 1), not in the class body.

Without this fix, IDEs and XSD validators reject the inline text syntax even though the generated StAX reader accepts it correctly.

Discovered while working on gnodet/maven#139.

gnodet added a commit to gnodet/maven that referenced this pull request Nov 8, 2026
…SD fix

The XSD for <options> and <alias> must carry mixed="true" on their
xs:complexType so that IDEs and schema validators accept both the inline
text syntax and the structured <arg>-child syntax documented for these
fields.

The fix is in codehaus-plexus/modello#628 (XsdGenerator: emit
mixed="true" for xml.format="mixed" fields). Pin to the SNAPSHOT until
that PR is released, then update to 2.8.2.
…d" fields

When a Modello field is annotated with xml.format="mixed", the
corresponding XML element accepts both free text and child elements.
The XSD generator must reflect this with mixed="true" on the
xs:complexType.

Two cases are handled:

1. Field with xml.format="mixed" and xml.tagName (wrapper element):
   the content lives inside the wrapper child element, so the anonymous
   xs:complexType written by writeListElement() gets mixed="true".

2. Field with xml.format="mixed" and no xml.tagName: the content is
   body text of the class element itself, so the named xs:complexType
   written by writeComplexTypeDescriptor() gets mixed="true". Fields
   with xml.tagName are explicitly excluded from this check because their
   mixed content lives inside a wrapper child element (case 1), not in
   the class body.

Without this fix, IDEs and XSD validators reject the inline text syntax
even though the generated StAX reader accepts it correctly.
@gnodet
gnodet force-pushed the fix/xsd-mixed-content-format branch from 530c211 to faff993 Compare November 8, 2026 23:15

@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 fix is correct — both code paths that emit mixed="true" are logically sound:

  • writeComplexTypeDescriptor: the guard m.getTagName() == null correctly excludes wrapper-element fields (Case 1), limiting mixed="true" to body-level class elements only (Case 2).
  • writeListElement: the anonymous xs:complexType inside a wrapper element correctly gets mixed="true" for xml.format="mixed" fields.

Null-safety is fine: getFormat() and getTagName() can both return null; the check uses "mixed".equals(...) and == null.

Missing test coverage. There is no test verifying the generated XSD. Consider adding a test model in modello-plugins/modello-plugin-xsd/src/test/resources/ with:

  • a field annotated xml.format="mixed" without xml.tagName (body-level / Case 2), and
  • a field annotated xml.format="mixed" with xml.tagName (wrapper / Case 1 — exercises writeListElement)

Then assert that mixed="true" appears on the class-level xs:complexType for the first case, and on the wrapper's inner xs:complexType for the second — and that it does not appear on unrelated xs:complexType elements.

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

Add MixedContentXsdGeneratorTest to verify that the XSD generator
emits mixed="true" on the correct xs:complexType elements when a
field uses xml.format="mixed":

- Case 2 (body-level): a String field with xml.format="mixed" and
  no xml.tagName triggers mixed="true" on the class-level named
  xs:complexType (MixedBody).
- Case 1 (wrapper): a List field with xml.format="mixed" and an
  explicit xml.tagName triggers mixed="true" on the anonymous
  xs:complexType inside the wrapper element (MixedWrapper).
- Control: a class with no mixed field (Plain) must NOT get
  mixed="true" on its xs:complexType.

@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 — the missing test coverage is now addressed. Good work adding both cases and a control assertion.

One issue in the new test:

assertNotNull(xsdFile, ...) does not check that the file was actually created. new File(path) never returns null — this assertion is always true whether the XSD was generated or not. A silent generation failure (e.g. wrong output directory) would leave the XSD file absent, doc = db.parse(xsdFile) would throw, and the error message would be "No such file or directory" instead of the intended "Generated XSD file must exist".

Replace with assertTrue(xsdFile.exists(), ...).

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

xml-apis:2.0.2 is a phantom version that Maven relocates to 1.0.b2,
which does not include org.w3c.dom.ElementTraversal. The class is part
of the JDK since Java 9, so the test passes on JDK 9+, but fails on
JDK 8. The last real release containing ElementTraversal is 1.4.01.

@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.

Previous finding addressed — assertTrue(xsdFile.exists(), ...) is in place. LGTM.

Pre-fix validation: static trace confirms the test was failing before the fix — without w.addAttribute("mixed", "true"), the XPath finds 0 complexType nodes with mixed="true", causing assertEquals(1, 0) to fail on both assertions.

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

@gnodet
gnodet merged commit e7dc022 into codehaus-plexus:master Nov 9, 2026
8 checks passed
@slachiewicz slachiewicz added the enhancement New feature or request label Nov 9, 2026
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.

3 participants