datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Support `x-propertyNames` in OpenAPI 3.0

Open indietyp opened this issue 2 years ago • 3 comments

Is your feature request related to a problem? Please describe.

OpenAPI 3.0 does not natively support the full set of the JSON-Schema specification; notably, patternProperties and propertyNames are absent. Some tools instead use the x- prefix to support patternProperties and propertyNames in OpenAPI 3.0.

Describe the solution you'd like

Would it be possible for datamodel-code-generator to support x-patternProperties and x-propertyNames as well? It would be great as we currently have no way of upgrading to 3.1 soon, but we would still like to benefit from pattern properties and properly typed property names.

Describe alternatives you've considered

There is no real alternative; the model currently generated is just empty, which is kind of valid, as in it will ignore any patterned properties.

Additional context

https://github.com/hashintel/hash/blob/990b911d4e9a166c4aff23844cf928945cd40159/apps/hash-graph/openapi/models/shared.json#L119-L149

A real-world snippet where we make use of both.

indietyp avatar Oct 02 '23 09:10 indietyp

@indietyp Could you please try the cli option? --field-extra-keys-without-x-prefix x-patternProperties

the CLI doesn't support propertyNames . we should support if we need it.

Thank you

koxudaxi avatar Oct 04 '23 16:10 koxudaxi

sadly adding this option didn't help. For the above definition the following is generated:

class PropertyTypeObject(BaseModel):
    """
    A JSON object where each entry is constrained by a property type.
    """

    model_config = ConfigDict(populate_by_name=True)

which doesn't reflect the x-patternProperties shown above. (Turns out that our reference was invalid in the schema, but upon fixing those, it stayed the same. Renaming from x-patternProperties to patternProperties solved our problem, but isn't really feasible.)

indietyp avatar Oct 09 '23 10:10 indietyp