Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/ruff-0.4.8
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Jun 13, 2024
2 parents fe22da8 + 301868b commit 7487f56
Show file tree
Hide file tree
Showing 20 changed files with 370 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.4.7'
rev: 'v0.4.8'
hooks:
- id: ruff
files: "^datamodel_code_generator|^tests"
Expand Down
58 changes: 40 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ and [msgspec.Struct](https://github.com/jcrist/msgspec) from an openapi file and
## Help
See [documentation](https://koxudaxi.github.io/datamodel-code-generator) for more details.

## Sponsors
[![JetBrains](https://avatars.githubusercontent.com/u/60931315?s=200&v=4)](https://github.com/JetBrainsOfficial)

## Quick Installation

To install `datamodel-code-generator`:
Expand Down Expand Up @@ -236,6 +233,45 @@ class Apis(BaseModel):
```
</details>
## Supported input types
- OpenAPI 3 (YAML/JSON, [OpenAPI Data Type](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.2.md#data-types));
- JSON Schema ([JSON Schema Core](http://json-schema.org/draft/2019-09/json-schema-validation.html)/[JSON Schema Validation](http://json-schema.org/draft/2019-09/json-schema-validation.html));
- JSON/YAML/CSV Data (it will be converted to JSON Schema);
- Python dictionary (it will be converted to JSON Schema);
- GraphQL schema ([GraphQL Schemas and Types](https://graphql.org/learn/schema/));
## Supported output types
- [pydantic](https://docs.pydantic.dev/1.10/).BaseModel;
- [pydantic_v2](https://docs.pydantic.dev/2.0/).BaseModel;
- [dataclasses.dataclass](https://docs.python.org/3/library/dataclasses.html);
- [typing.TypedDict](https://docs.python.org/3/library/typing.html#typing.TypedDict);
- [msgspec.Struct](https://github.com/jcrist/msgspec);
- Custom type from your [jinja2](https://jinja.palletsprojects.com/en/3.1.x/) template;
## Sponsors
<table>
<tr>
<td valign="top" align="center">
<a href="https://github.com/JetBrainsOfficial">
<img src="https://avatars.githubusercontent.com/u/60931315?s=100&v=4" alt="JetBrains Logo" style="width: 100px;">
<p>JetBrains</p>
</a>
</td>
<td valign="top" align="center">
<a href="https://github.com/astral-sh">
<img src="https://avatars.githubusercontent.com/u/115962839?s=200&v=4" alt="Astral Logo" style="width: 100px;">
<p>Astral</p>
</a>
</td>
<td valign="top" align="center">
<a href="https://github.com/DataDog">
<img src="https://avatars.githubusercontent.com/u/365230?s=200&v=4" alt="Datadog, Inc. Logo" style="width: 100px;">
<p>Datadog, Inc.</p>
</a>
</td>
</tr>
</table>
## Projects that use datamodel-code-generator
These OSS projects use datamodel-code-generator to generate many models.
Expand Down Expand Up @@ -267,21 +303,6 @@ See the following linked projects for real world examples and inspiration.
- [SeldonIO/MLServer](https://github.com/SeldonIO/MLServer)
- *[generate-types.sh](https://github.com/SeldonIO/MLServer/blob/master/hack/generate-types.sh)*

## Supported input types
- OpenAPI 3 (YAML/JSON, [OpenAPI Data Type](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.2.md#data-types));
- JSON Schema ([JSON Schema Core](http://json-schema.org/draft/2019-09/json-schema-validation.html)/[JSON Schema Validation](http://json-schema.org/draft/2019-09/json-schema-validation.html));
- JSON/YAML/CSV Data (it will be converted to JSON Schema);
- Python dictionary (it will be converted to JSON Schema);
- GraphQL schema ([GraphQL Schemas and Types](https://graphql.org/learn/schema/));

## Supported output types
- [pydantic](https://docs.pydantic.dev/1.10/).BaseModel;
- [pydantic_v2](https://docs.pydantic.dev/2.0/).BaseModel;
- [dataclasses.dataclass](https://docs.python.org/3/library/dataclasses.html);
- [typing.TypedDict](https://docs.python.org/3/library/typing.html#typing.TypedDict);
- [msgspec.Struct](https://github.com/jcrist/msgspec);
- Custom type from your [jinja2](https://jinja.palletsprojects.com/en/3.1.x/) template;

## Installation

To install `datamodel-code-generator`:
Expand Down Expand Up @@ -319,6 +340,7 @@ This method needs the [http extra option](#http-extra-option)
## All Command Options

The `datamodel-codegen` command:

```bash
usage:
datamodel-codegen [options]
Expand Down
16 changes: 14 additions & 2 deletions datamodel_code_generator/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ def __change_from_import(
from_, import_ = full_path = relative(
model.module_name, data_type.full_name
)
import_ = import_.replace('-', '_')

alias = scoped_model_resolver.add(full_path, import_).name

Expand Down Expand Up @@ -778,8 +779,18 @@ def __apply_discriminator_type(
discriminator_model.path.split('#/')[-1]
!= path.split('#/')[-1]
):
# TODO: support external reference
continue
if (
path.startswith('#/')
or discriminator_model.path[:-1]
!= path.split('/')[-1]
):
t_path = path[str(path).find('/') + 1 :]
t_disc = discriminator_model.path[
: str(discriminator_model.path).find('#')
].lstrip('../')
t_disc_2 = '/'.join(t_disc.split('/')[1:])
if t_path != t_disc and t_path != t_disc_2:
continue
type_names.append(name)
else:
type_names = [discriminator_model.path.split('/')[-1]]
Expand Down Expand Up @@ -1250,6 +1261,7 @@ class Processed(NamedTuple):
init = True
else:
module = (*module[:-1], f'{module[-1]}.py')
module = tuple(part.replace('-', '_') for part in module)
else:
module = ('__init__.py',)

Expand Down
56 changes: 38 additions & 18 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ This code generator creates [pydantic v1 and v2](https://docs.pydantic.dev/) mod
[![Pydantic v1](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v1.json)](https://pydantic.dev)
[![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)](https://pydantic.dev)

## Sponsors
[![JetBrains](https://avatars.githubusercontent.com/u/60931315?s=200&v=4)](https://github.com/JetBrainsOfficial)

## Quick Installation

To install `datamodel-code-generator`:
Expand Down Expand Up @@ -232,6 +229,44 @@ class Apis(BaseModel):
```
</details>
## Supported input types
- OpenAPI 3 (YAML/JSON, [OpenAPI Data Type](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#data-types));
- JSON Schema ([JSON Schema Core](http://json-schema.org/draft/2019-09/json-schema-validation.html)/[JSON Schema Validation](http://json-schema.org/draft/2019-09/json-schema-validation.html));
- JSON/YAML/CSV Data (it will be converted to JSON Schema);
- Python dictionary (it will be converted to JSON Schema);
- GraphQL schema ([GraphQL Schemas and Types](https://graphql.org/learn/schema/));
## Supported output types
- [pydantic](https://docs.pydantic.dev/1.10/).BaseModel;
- [pydantic_v2](https://docs.pydantic.dev/2.0/).BaseModel;
- [dataclasses.dataclass](https://docs.python.org/3/library/dataclasses.html);
- [typing.TypedDict](https://docs.python.org/3/library/typing.html#typing.TypedDict);
- [msgspec.Struct](https://github.com/jcrist/msgspec);
- Custom type from your [jinja2](https://jinja.palletsprojects.com/en/3.1.x) template;
## Sponsors
<table>
<tr>
<td valign="top" align="center">
<a href="https://github.com/JetBrainsOfficial">
<img src="https://avatars.githubusercontent.com/u/60931315?s=100&v=4" alt="JetBrains Logo" style="width: 100px;">
<p>JetBrains</p>
</a>
</td>
<td valign="top" align="center">
<a href="https://github.com/astral-sh">
<img src="https://avatars.githubusercontent.com/u/115962839?s=200&v=4" alt="Astral Logo" style="width: 100px;">
<p>Astral</p>
</a>
</td>
<td valign="top" align="center">
<a href="https://github.com/DataDog">
<img src="https://avatars.githubusercontent.com/u/365230?s=200&v=4" alt="Datadog, Inc. Logo" style="width: 100px;">
<p>Datadog, Inc.</p>
</a>
</td>
</tr>
</table>
## Projects that use datamodel-code-generator
These OSS projects use datamodel-code-generator to generate many models.
Expand Down Expand Up @@ -263,21 +298,6 @@ See the following linked projects for real world examples and inspiration.
- [SeldonIO/MLServer](https://github.com/SeldonIO/MLServer)
- *[generate-types.sh](https://github.com/SeldonIO/MLServer/blob/master/hack/generate-types.sh)*

## Supported input types
- OpenAPI 3 (YAML/JSON, [OpenAPI Data Type](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#data-types));
- JSON Schema ([JSON Schema Core](http://json-schema.org/draft/2019-09/json-schema-validation.html)/[JSON Schema Validation](http://json-schema.org/draft/2019-09/json-schema-validation.html));
- JSON/YAML/CSV Data (it will be converted to JSON Schema);
- Python dictionary (it will be converted to JSON Schema);
- GraphQL schema ([GraphQL Schemas and Types](https://graphql.org/learn/schema/));

## Supported output types
- [pydantic](https://docs.pydantic.dev/1.10/).BaseModel;
- [pydantic_v2](https://docs.pydantic.dev/2.0/).BaseModel;
- [dataclasses.dataclass](https://docs.python.org/3/library/dataclasses.html);
- [typing.TypedDict](https://docs.python.org/3/library/typing.html#typing.TypedDict);
- [msgspec.Struct](https://github.com/jcrist/msgspec);
- Custom type from your [jinja2](https://jinja.palletsprojects.com/en/3.1.x) template;

## Installation

To install `datamodel-code-generator`:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# generated by datamodel-codegen:
# filename: schema.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Optional, Union

from pydantic import BaseModel, Field
from typing_extensions import Literal


class Type1(BaseModel):
type_: Literal['a'] = Field('a', title='Type ')


class Type2(BaseModel):
type_: Literal['b'] = Field('b', title='Type ')
ref_type: Optional[Type1] = Field(None, description='A referenced type.')


class Type4(BaseModel):
type_: Literal['d'] = Field('d', title='Type ')


class Type5(BaseModel):
type_: Literal['e'] = Field('e', title='Type ')


class Type3(BaseModel):
type_: Literal['c'] = Field('c', title='Type ')


class Response(BaseModel):
inner: Union[Type1, Type2, Type3, Type4, Type5] = Field(
..., discriminator='type_', title='Inner'
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# generated by datamodel-codegen:
# filename: discriminator_with_external_reference
# timestamp: 2019-07-26T00:00:00+00:00
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# generated by datamodel-codegen:
# filename: discriminator_with_external_reference
# timestamp: 2019-07-26T00:00:00+00:00
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# generated by datamodel-codegen:
# filename: discriminator_with_external_reference
# timestamp: 2019-07-26T00:00:00+00:00
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# generated by datamodel-codegen:
# filename: inner_folder/artificial_folder/type-1.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel, Field
from typing_extensions import Literal


class Type1(BaseModel):
type_: Literal['a'] = Field(..., const=True, title='Type ')
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# generated by datamodel-codegen:
# filename: inner_folder/schema.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Union

from pydantic import BaseModel, Field
from typing_extensions import Literal

from .. import type_4
from ..subfolder import type_5
from . import type_2
from .artificial_folder import type_1


class Type3(BaseModel):
type_: Literal['c'] = Field(..., const=True, title='Type ')


class Response(BaseModel):
inner: Union[type_1.Type1, type_2.Type2, Type3, type_4.Type4, type_5.Type5] = Field(
..., discriminator='type_', title='Inner'
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# generated by datamodel-codegen:
# filename: inner_folder/type-2.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel, Field

from .artificial_folder import type_1


class Type2(BaseModel):
type_: Literal['b'] = Field(..., const=True, title='Type ')
ref_type: Optional[type_1.Type1] = Field(None, description='A referenced type.')
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# generated by datamodel-codegen:
# filename: discriminator_with_external_reference
# timestamp: 2019-07-26T00:00:00+00:00
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# generated by datamodel-codegen:
# filename: subfolder/type-5.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel, Field


class Type5(BaseModel):
type_: Literal['e'] = Field(..., const=True, title='Type ')
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# generated by datamodel-codegen:
# filename: type-4.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel, Field


class Type4(BaseModel):
type_: Literal['d'] = Field(..., const=True, title='Type ')
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"properties": {
"type_": {
"const": "a",
"default": "a",
"title": "Type "
}
},
"title": "Type1",
"type": "object"
}
Loading

0 comments on commit 7487f56

Please sign in to comment.