unioslo / harborapi

Python async client for the Harbor REST API v2.0.
https://unioslo.github.io/harborapi/
MIT License
27 stars 5 forks source link

Fix model fields with `example=` kwargs #39

Open pederhan opened 1 year ago

pederhan commented 1 year ago

datamodel-code-generator generates pydantic.Field attributes with the kwarg example which contains a single example value for the field:

https://github.com/pederhan/harborapi/blob/705903fabc44c2e381bab55a75e1847a77032b98/harborapi/models/_models.py#L182-L191

However, in Pydantic V2, this is now called examples¹ ² and is a list of examples. The Scanner snippet above should be changed to the following:

class Scanner(BaseModel):
    name: Optional[str] = Field(
        None, description="Name of the scanner", examples=["Trivy"]
    )
    vendor: Optional[str] = Field(
        None, description="Name of the scanner provider", examples=["Aqua Security"]
    )
    version: Optional[str] = Field(
        None, description="Version of the scanner adapter", examples=["v0.9.1"]
    )

If this isn't fixed in datamodel-code-generator soon, we either have to try to contribute the fix ourselves, or we have to perform some AST manipulation to rewrite all instances of example.


1: https://docs.pydantic.dev/latest/api/fields/#pydantic.fields.Field 2: https://docs.pydantic.dev/latest/concepts/fields/#customizing-json-schema