glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
11.76k stars 1.04k forks source link

feat(python): add --pydantic-base-model option #2604

Open alpoi-x opened 1 month ago

alpoi-x commented 1 month ago

Description

Adds a python option pydantic-base-model, which will generate classes inheriting pydantic's BaseModel.

Related Issue

https://github.com/glideapps/quicktype/issues/1474

Motivation and Context

Pydantic is a commonly used python module for defining schemas and dataclasses with validation. It is used in FastApi. It is a nice alternative to the builtin dataclasses module.

Previous Behaviour / Output

Quicktype generates standard classes or dataclasses (3.7+) for python

New Behaviour / Output

With the --pydantic-base-model option, it generates pydantic BaseModel classes

How Has This Been Tested?

Built and generated python from a schema, with and without --pydantic-base-model option.

Without the option included from dataclasses import dataclass and @dataclass

With the option instead included from pydantic import BaseModel and class Example(BaseModel)

Screenshots (if appropriate):

zhao004 commented 3 weeks ago

Description

添加一个 python 选项 pydantic-base-model ,它将生成继承 pydantic 的 BaseModel 的类。

Related Issue

1474

Motivation 和 Context

Pydantic 是一个常用的 python 模块,用于通过验证定义模式和数据类。它在 FastApi 中使用。它是内置 dataclasses 模块的一个很好的替代品。

上一页 Behaviour / Output

Quicktype 为 python 生成标准类或数据类 (3.7+)

创建 Behaviour / Output

使用 --pydantic-base-model 选项,它生成 pydantic BaseModel 类

How Has This Been Tested?

从模式构建并生成 python,带或不带 --pydantic-base-model 选项。

没有包含 from dataclasses import dataclass@dataclass 选项

选项包含 from pydantic import BaseModelclass Example(BaseModel)

Screenshots (if appropriate):


搜狗截图20240611112248

搜狗截图20240611111916 This option does not exist.

alpoi-x commented 5 days ago

@zhao004 This is a pull request - code and changes included here are not part of the application until they are merged.

zhao004 commented 4 days ago

@zhao004 This is a pull request - code and changes included here are not part of the application until they are merged.

When can the merger be completed?