Ability to generate pydantic models from jsonschema. swaggerapi/openapi-codegen libraries don't do a very good job creating client models that validate themselves -- I would like to be able to generate pydantic models from a jsonschema.
This issue relates specifically to pydantic model code generation, see this comment below.
@timothyjlaurent
I agree too. It's a good idea to improve speed for developing API.
Also, I have the same idea, and I'm creating the code generator. The repository closed project yet.
I have opened the project for example.
https://github.com/koxudaxi/datamodel_code_generator
But, We should add a feature to create a code generator. https://github.com/samuelcolvin/pydantic/issues/507
I'm making a pull request for the feature. https://github.com/samuelcolvin/pydantic/pull/628
@samuelcolvin The code generator will be a great advantage on pydantic.
I will implement and upload pypi the code generator for pip after custom root type feature will be merged.
However, I think the best is you make and distribute the code generator as an official project.
since it's a standalone feature and won't effect anyone not using it, I'd be happy to accept a PR implementing a model generator.
I'd love to have this feature!
/cc @motinani @worroc
I started a local experiment to generate Pydantic models from JSON Schema, using Jinja2 templates, and used in a recent project. I'm still not generating models that reference other models.
But I realized that probably a better approach would be to have two separated features (or packages if not suitable for Pydantic?):
create_model, generate the equivalent Python code as a text string e.g.:from pydantic import BaseModel, Schema
class SomeDynamicModel(BaseModel):
some_str_field: str
some_int_field: int = Schema(..., title="Some title")
The two features combined would result in being able to generate Pydantic models from JSON Schema.
But the separated components could be extended to, e.g.:
I'm just brainstorming here, but some of these ideas might be useful in case someone ( :wink: @koxudaxi ) implements it before me...
@tiangolo
Thank you for showing us your idea.
I have not thought about using the dynamic model creation with create_model.
I may use the way to develop it.
Generate dynamic Pydantic models from DB (e.g. SQLAlchemy) models and then generate the Python code Pydantic models.
I agree. I guess the code generator needs multi-type parsers for OpenAPI, json-schema, SQL DDL or SQLAlchemy model.
I will implement this experimental idea next time :)
parser --> pydantic.create_model --> code_generator
Okay, having thought about this more I think it should be outside pydantic, a separate package.
I'd be happy to link to it, but given the amount of interest in pydantic I'm already struggling to deal with the load. Since this is easily split, best to keep it complete separate.
Also if you're using jinja you'll have completely separate dependencies.
@samuelcolvin
I'd be happy to link to it, but given the amount of interest in pydantic I'm already struggling to deal with the load. Since this is easily split, best to keep it complete separate.
If I create the code generator, then Where do I uploading it on GitHub?
This repogitory of pydantic?
Also if you're using jinja you'll have completely separate dependencies.
Did you think the code generator should not import pydantic module?
We need a structure to build pydantic code which has class name and field, schema.
I think we can use pydantic.BaseModel and pydantic.fields.Field as the structure.
If I create the code generator, then Where do I uploading it on GitHub?
Your own repo, and upload to pypi under your own account.
Did you think the code generator should not import pydantic module?
I'm afraid I don't know what you mean.
There are two possible approaches, I have no preference on which (or both) to use:
create_modelThank you for your reply.
I understand.
Hi folks! Has there been any progress on this?
Maybe my use-case is simpler -- using schema we can easily serialize a models definition. Is there a way to deserialize it into a model class as well? E.G. a BaseModel.from_schema method?
No progress as far as I know.
Might be possible using create_model(), I would review a PR if someone worked on it.
Hi! I stumbled on this while researching for a project at work. I've built a proof of concept before (also using Jinja, but targeting marshmallow instead) and could _maybe_ put out some kind of beta in the next week or so. If @koxudaxi has made substantial progress I would rather contribute to that... :smile: ?
I'm using @koxudaxi 's tool for an internal project (an openapi client) and it's been _very_ useful!
I implemented JasonSchema parser on my code-generator.
It means the tool can generate a pydantic model from JsonSchema since version 0.3.0.
The version was released on Jan 10, 2020.
https://github.com/koxudaxi/datamodel-code-generator
(I use the tool a few internal projects)
@afiorillo
@joshbode contributes a lot of PR on my code-generator project. :wink:
We're welcome to your contribution. It will help the pydantic community.
There are two potential routes here:
create_model(). This could potentially be added to the pydantic library if someone wants to work on it.The original question here doesn't explain which approach they're looking for, but many of the comments indicate interest in 1. Therefore let's leave this issue to relate to code generation.
If you'd like to create dynamic pydantic models based on JsonSchema, please create a new issue.
@samuelcolvin Thanks for following up! Those solutions make sense. The code-generation makes solves a variety of use-cases (including ours). I'll think more about it and see how useful (2) would be and create another issue if its something we'd want to implement/need.
Most helpful comment
There are two potential routes here:
create_model(). This could potentially be added to the pydantic library if someone wants to work on it.The original question here doesn't explain which approach they're looking for, but many of the comments indicate interest in 1. Therefore let's leave this issue to relate to code generation.
If you'd like to create dynamic pydantic models based on JsonSchema, please create a new issue.