Problem
When a JSON Schema has a property with items containing allOf references, datamodel-code-generator (v0.51.0 and v0.54.1) incorrectly promotes properties from the nested subobject schema into the parent class.
Example
ComposedUnit schema has:
- Top-level properties:
type, main_symbol, conversion_factor_from_si, ucum_codes, factor_units, system_of_quantities_and_units, composed_units
composed_units.items (ComposedUnitElement) has its own properties including osw_id, conversion_factor_to_main_unit
Expected:
class ComposedUnit(Item, OntologyRelated):
# Only top-level properties
main_symbol: str
conversion_factor_from_si: float
factor_units: list[FactorUnit]
composed_units: list[ComposedUnitElement]
class ComposedUnitElement(OntologyRelated):
osw_id: str
conversion_factor_to_main_unit: float
factor_units: list[ComposedFactorUnit]
Actual:
class ComposedUnit(Item, OntologyRelated):
main_symbol: str
conversion_factor_from_si: float
factor_units: list[FactorUnit]
composed_units: list[ComposedUnitElement]
osw_id: str # LEAKED from ComposedUnitElement
conversion_factor_to_main_unit: float # LEAKED from ComposedUnitElement
Root cause
The temp schema files written by osw-python's _fetch_schema are correct (no leaked properties at top level). The leak happens inside datamodel-code-generator during allOf resolution of nested items schemas. The nested composed_units.items.allOf to OntologyRelated causes its resolved properties to bubble up into the parent class.
Same issue affects PrefixUnit (leaks from prefix_units.items) and QuantityUnit (leaks from composed_units.items).
Additional allOf issues in datamodel-code-generator
In the same codebase, we also observed:
- Dropped allOf base class:
ComposedUnit(Item, OntologyRelated) is generated as ComposedUnit(OntologyRelated) - the Item base is dropped despite both being in allOf
- Wrong type default:
ComposedUnit gets QuantityUnit's type default instead of its own
Both are non-deterministic and seem related to schema processing order.
Current workaround
Manual fix of generated _model.py files + post-processing in osw-python-package-generator.
Affected versions
- datamodel-code-generator 0.51.0 and 0.54.1
- oold-python (uses datamodel-code-generator with
allof_class_hierarchy=Always, reuse_model=True, use_title_as_name=True)
Related
Problem
When a JSON Schema has a property with
itemscontainingallOfreferences,datamodel-code-generator(v0.51.0 and v0.54.1) incorrectly promotes properties from the nested subobject schema into the parent class.Example
ComposedUnit schema has:
type,main_symbol,conversion_factor_from_si,ucum_codes,factor_units,system_of_quantities_and_units,composed_unitscomposed_units.items(ComposedUnitElement) has its own properties includingosw_id,conversion_factor_to_main_unitExpected:
Actual:
Root cause
The temp schema files written by osw-python's
_fetch_schemaare correct (no leaked properties at top level). The leak happens insidedatamodel-code-generatorduringallOfresolution of nesteditemsschemas. The nestedcomposed_units.items.allOfto OntologyRelated causes its resolved properties to bubble up into the parent class.Same issue affects
PrefixUnit(leaks fromprefix_units.items) andQuantityUnit(leaks fromcomposed_units.items).Additional allOf issues in datamodel-code-generator
In the same codebase, we also observed:
ComposedUnit(Item, OntologyRelated)is generated asComposedUnit(OntologyRelated)- theItembase is dropped despite both being inallOfComposedUnitgetsQuantityUnit's type default instead of its ownBoth are non-deterministic and seem related to schema processing order.
Current workaround
Manual fix of generated
_model.pyfiles + post-processing inosw-python-package-generator.Affected versions
allof_class_hierarchy=Always,reuse_model=True,use_title_as_name=True)Related