fix: enforce minProperties on generated models#55
Open
vishkaty wants to merge 1 commit into
Open
Conversation
…otocol#49) datamodel-code-generator drops minProperties on object schemas with declared properties, so Description() validated with zero fields in violation of description.json's minProperties: 1. (minProperties on free-form object properties is unaffected — the generator already maps those to Field(min_length=...) on the dict field.) A new post-generation step (postprocess_models.py, wired into generate_models.sh before formatting) scans the preprocessed schemas for root-level minProperties constraints and injects a model_validator(mode="after") into each matching generated class. JSON Schema counts the keys present on the object, so the validator counts provided fields (model_fields_set) unioned with extra keys (model_extra): an explicit null is a present key, and unknown keys on extra="allow" models count too. The step is idempotent, data-driven from the schemas (future minProperties additions are picked up on regeneration), and fails generation loudly if a constraint can't be mapped to a generated class. The committed Description model is regenerated with the validator. Tests cover the injector (dependency-free) and the enforced semantics on Description; the workflow now installs the package so those semantic tests run in CI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #49:
datamodel-code-generatordropsminPropertieson object schemas with declared properties, soDescription()validates with zero fields in violation ofdescription.json'sminProperties: 1. (The free-form-object case is unaffected — the generator already maps that toField(min_length=...)on the dict field, e.g.AvailablePaymentInstrument.constraints.)Implements the post-processing option proposed in the issue: a new
postprocess_models.pystep (wired intogenerate_models.shbetween generation and formatting) scans the preprocessed schemas for root-levelminPropertiesconstraints and injects amodel_validator(mode="after")into each matching generated class.Counting semantics follow JSON Schema (keys present on the object), verified empirically against pydantic: provided fields (
model_fields_set) unioned with extra keys (model_extra) — an explicitnullis a present key, and unknown keys onextra="allow"models count too; the union avoids double-counting since pydantic includes extras inmodel_fields_set.Properties of the step:
minPropertiesadditions to any schema are picked up on regeneration, no code change needed (currentlydescription.jsonis the only root-level case onucpmain; there are no nested property-carrying cases).Descriptionmodel is regenerated with the validator.Validation
tests/test_min_properties.py: dependency-free injector tests (insertion, idempotency, schema scan, multi-class modules) + semantic tests on the realDescriptionmodel (empty rejected; single field, explicit-null key, and extra-only key accepted). Fullunittest discover: 23/23 green.pip install -e .so the semantic tests execute in CI rather than skipping.ruff format+ruff checkclean.