Skip to content

fix(xmlschema): write optional properties with minOccurs="0" - #307

Merged
mttrbrts merged 1 commit into
accordproject:mainfrom
NopeCharon:nopecharon/i304/xmlschema-optional-properties
Nov 11, 2026
Merged

mttrbrts merged 1 commit into
accordproject:mainfrom
NopeCharon:nopecharon/i304/xmlschema-optional-properties

Conversation

@NopeCharon

Copy link
Copy Markdown
Contributor

Closes #304

Generated XML Schema now lets an instance leave out optional properties. Before this change, every optional field and relationship was written as a required element.

Changes

  • Write minOccurs="0" for optional fields and relationships, and on the wrapper element of an optional map field that has a size validator.
  • Pass the property's optional modifier into visitField for scalar fields, because the field returned by getScalarField() does not carry it.
  • Move the minOccurs/maxOccurs logic shared by fields and relationships into a toOccurs helper.
  • Add unit tests for optional fields, scalar fields, arrays, map fields and relationships, and update the hr snapshot (state, the map fields of Company, middleNames, manager).

Flags

  • An optional array with a size validator minimum (o String[] tags size=[2,5] optional) now gets minOccurs="0". A repeated element can't tell an absent array apart from an empty one, and an absent optional array has to validate, so the minimum can't be enforced in this case.
  • bug(xmlschema): schemas for models with maps fail to compile #305 also changes the map-field branch of visitField, a few lines from this change. Whichever PR is merged second will need a small rebase.
  • I checked the output with xmllint --schema: an instance without its optional properties fails on main (Missing child element(s). Expected is ( {org.acme}nick )) and validates with this change.

Screenshots or Video

N/A

Related Issues

Author Checklist

  • Ensure you provide a DCO sign-off for your commits using the --signoff option of git commit.
  • Vital features and changes captured in unit and/or integration tests
  • Commits messages follow AP format
  • Extend the documentation, if necessary
  • Merging to main from fork:branchname

XmlSchemaVisitor ignored isOptional(), so every optional field and
relationship was written as a plain xs:element. XSD defaults minOccurs
to 1, which made optional properties required: an instance leaving out
an optional property failed validation.

- Write minOccurs="0" for optional fields and relationships, and on the
  wrapper element of an optional map field with a size validator.
- Optional arrays always get minOccurs="0", even when their size
  validator has a minimum, because an absent array is valid.
- Pass the property's optional modifier into visitField for scalar
  fields. The field returned by getScalarField() does not carry it.
- Move the occurrence attribute logic shared by fields and
  relationships into a toOccurs helper.

Closes accordproject#304

Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>
@mira-accord-project

Copy link
Copy Markdown

Thanks for your contribution, and for opening #304 with the details alongside this PR. The CI workflows have been approved and are passing. The PR now waits for review from the Concerto maintainers. If you push more changes, the checks will run again, and it helps if the commits are signed off (DCO).

Drafted by an Accord Project agent, reviewed by a maintainer.

@mira-accord-project mira-accord-project 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.

Thank you for this fix, and for a first contribution that is so carefully done. Writing minOccurs="0" for optional properties makes the generated schema match what Concerto allows. The new toOccurs helper puts the rule for fields, relationships and map fields in one place. Passing the property's isOptional() through for scalar-typed properties is a good catch, because the scalar field doesn't carry that modifier. Treating optional arrays as minOccurs="0" regardless of a declared minimum size is a sensible choice, since XSD can't express "absent, or at least n". The new unit tests cover each of these branches, and the snapshot changes all follow from the fix. I found nothing that blocks merging. One small coordination point is below. Once a maintainer has reviewed it and the ordering with the other open XML Schema change is settled, this looks ready to go.

Nothing blocking; some suggestions below. Findings: 1 nit.


Automated review by Mira, an Accord Project agent, at faab461. Findings are labelled blocking, should-fix or nit; a maintainer makes the final call.

}
const mapName = field.getType();
parameters.fileWriter.writeLine(2, `<xs:element name="${this.toXsName(field.getName())}">`);
parameters.fileWriter.writeLine(2, `<xs:element name="${this.toXsName(field.getName())}"${this.toOccurs(field, isOptional)}>`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit · hygiene

Pull request #308 also edits the map branch of visitField: it changes the const mapName line directly above this <xs:element name=...> line, and edits the same visitField map tests. Whichever of the two merges second will probably hit a merge conflict. There is nothing to change in this PR's logic. It would help to agree an order with #308 and rebase the other one afterwards, re-running npm run test:updateSnapshots if the snapshot needs regenerating.

@mttrbrts
mttrbrts merged commit 095542a into accordproject:main Nov 11, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(xmlschema): optional properties are generated as required elements

2 participants