Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class level json_schema_extra #2129

Open
JoanPuig opened this issue Oct 21, 2024 · 0 comments
Open

Class level json_schema_extra #2129

JoanPuig opened this issue Oct 21, 2024 · 0 comments

Comments

@JoanPuig
Copy link

Given the following specification:

info:
  title: Example API
  version: '1.0'
openapi: 3.0.0
components:
  schemas:
    MyType:
      x-type-metadata:
        type-metadata-1: type md 1 value
        type-metadata-2: type md 2 value
      type: object
      properties:
        myProperty:
          type: string
          x-field-metadata:
            field-metadata-1: field md 1 value
            field-metadata-2: field md 2 value

and the following code to generate the Pydantic model:

generate(
    openapi_yaml,
    input_file_type=InputFileType.OpenAPI,
    output_model_type=DataModelType.PydanticV2BaseModel,
    target_python_version=PythonVersion.PY_312,
    use_double_quotes=True,
    disable_timestamp=True,
    set_default_enum_member=True,
    collapse_root_models=True,
    allow_extra_fields=False,
    use_schema_description=True,
    use_field_description=True,
    field_extra_keys_without_x_prefix={
        "x-type-metadata",
        "x-field-metadata",
    },
)

Generates the following:

# generated by datamodel-codegen:
#   filename:  <stdin>

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel, Field


class MyType(BaseModel):
    myProperty: Optional[str] = Field(
        None,
        json_schema_extra={
            "field-metadata": {
                "field-metadata-1": "field md 1 value",
                "field-metadata-2": "field md 2 value",
            }
        },
    )

It would be very useful to have the type level "x-*" (in the above example "x-type-metadata") metadata included in the generated code in a similar way that the field metadata is preserved in the json_schema_extra property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@JoanPuig and others