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

Evaluate code gen to build pydantic models from JSON schema #84

Closed
geospatial-jeff opened this issue Jun 8, 2021 · 3 comments
Closed

Comments

@geospatial-jeff
Copy link
Collaborator

@kylebarron mentioned this project is becoming more mature, might be worth looking into how useful it is here.
Would be really nice to programmatically generate pydantic models from the JSON schema fragments provided by the spec.

https://github.com/koxudaxi/datamodel-code-generator

@kylebarron
Copy link
Contributor

Some notes copied from slack. From inside item-spec/json-schema try:

datamodel-codegen \
  --input item.json \
  --input-file-type jsonschema --class-name Item \
  --output item \
  `# Alias camelcase fields to snake case for Python` \
  --snake-case-field \
  `# Set top-level "description" field as Python class docstring` \
  --use-schema-description \
  `# Allow creating the Pydantic model using both the alias and the field name` \
  --allow-population-by-field-name

And the output pydantic models will be in the item folder (with an __init__.py so you can just do import item.

It’s not perfect still... for example the datetime field is created as a str type instead of a datetime type:

    datetime: Optional[Optional[str]] = Field(
        None,
        description='The searchable date/time of the assets, in UTC (Formatted in RFC 3339) ',
        title='Date and Time',
    )

This is because the source JSON Schema has format: date-time but also has "type": ["string", "null"],. Adding null into the allowed types trips up the generator. Compare that to the other fields in the spec that are declared as only type: string, which are accurately set as datetime:

    start_datetime: Optional[datetime] = Field(
        None,
        description='The searchable start date/time of the assets, in UTC (Formatted in RFC 3339) ',
        title='Start Date and Time',
    )
    end_datetime: Optional[datetime] = Field(
        None,
        description='The searchable end date/time of the assets, in UTC (Formatted in RFC 3339) ',
        title='End Date and Time',
    )
    created: Optional[datetime] = Field(None, title='Creation Time')
    updated: Optional[datetime] = Field(None, title='Last Update Time')

@kylebarron
Copy link
Contributor

One thing that might be difficult with Pydantic is its support for model unions in relation to extensions. In a language like typescript, you could do Item & EOExtension to declare the type of an item that also matches the EO extension spec. I'm not sure how easy that would be in Pydantic. Though I also don't know exactly how extensions are currently handled.

@vincentsarago
Copy link
Member

duplicated of #50

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

3 participants