Skip to content

Commit

Permalink
🚑 fixes graphql parser --use-standard-collections --use-union-operato…
Browse files Browse the repository at this point in the history
…r --use-annotated (#2016)

* 🩹 fixes graphql parser use-standard-collections and use-union-operator

* 🩹 fixes graphql parser to use annoteted on typename fields

* ♻️ refactors tests

* ♻️ refactors tests

Signed-off-by: bpsoos <soos.peter.96@gmail.com>

* ♻️ refactors tests

Signed-off-by: bpsoos <soos.peter.96@gmail.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* 🩹 fixes failing test

Signed-off-by: bpsoos <soos.peter.96@gmail.com>

* ♻️ refactors test_main into multiple files

Signed-off-by: bpsoos <soos.peter.96@gmail.com>

* ♻️ refactors test_main into multiple files

Signed-off-by: bpsoos <soos.peter.96@gmail.com>

* 🩹 fixes msgspec output file name

Signed-off-by: bpsoos <soos.peter.96@gmail.com>

* ♻️ refactors jsonschema expectations

Signed-off-by: bpsoos <soos.peter.96@gmail.com>

* ♻️ refactors jsonschema expectations

Signed-off-by: bpsoos <soos.peter.96@gmail.com>

* ♻️ refactors jsonschema expectations

Signed-off-by: bpsoos <soos.peter.96@gmail.com>

* ♻️ refactors openapi expectations

Signed-off-by: bpsoos <soos.peter.96@gmail.com>

* ♻️ refactors csv json yaml and general expectations

Signed-off-by: bpsoos <soos.peter.96@gmail.com>

* Fix test coverage (#2020)

* Fix test coverage

* Fix test coverage

* Update output.py

* Fix test coverage

* Fix coverage

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix coverage

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix coverage

* Fix coverage

* Fix coverage

* Fix coverage

* Fix coverage

* Fix coverage

* Fix coverage

* Fix coverage

* Fix coverage

* Fix coverage

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Fix coverage (#2023)

* Fix coverage

* Fix coverage

* docs: Update airbyte use case + fix broken link (#2021)

Co-authored-by: Koudai Aono <koxudaxi@gmail.com>

* formats

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Signed-off-by: bpsoos <soos.peter.96@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Koudai Aono <koxudaxi@gmail.com>
Co-authored-by: Natik Gadzhi <natik@respawn.io>
  • Loading branch information
4 people authored Jul 4, 2024
1 parent 202c0b8 commit e4fa23a
Show file tree
Hide file tree
Showing 427 changed files with 7,146 additions and 6,997 deletions.
27 changes: 19 additions & 8 deletions datamodel_code_generator/parser/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@
escape_characters,
)
from datamodel_code_generator.reference import ModelType, Reference
from datamodel_code_generator.types import (
DataTypeManager,
StrictTypes,
Types,
)
from datamodel_code_generator.types import DataTypeManager, StrictTypes, Types

try:
import graphql
Expand Down Expand Up @@ -233,6 +229,8 @@ def __init__(

self.data_model_scalar_type = data_model_scalar_type
self.data_model_union_type = data_model_union_type
self.use_standard_collections = use_standard_collections
self.use_union_operator = use_union_operator

def _get_context_source_path_parts(self) -> Iterator[Tuple[Source, List[str]]]:
# TODO (denisart): Temporarily this method duplicates
Expand Down Expand Up @@ -283,8 +281,13 @@ def _resolve_types(self, paths: List[str], schema: graphql.GraphQLSchema) -> Non
def _typename_field(self, name: str) -> DataModelFieldBase:
return self.data_model_field_type(
name='typename__',
data_type=DataType(literals=[name]),
data_type=DataType(
literals=[name],
use_union_operator=self.use_union_operator,
use_standard_collections=self.use_standard_collections,
),
default=name,
use_annotated=self.use_annotated,
required=False,
alias='__typename',
use_one_literal_as_default=True,
Expand Down Expand Up @@ -348,15 +351,23 @@ def parse_field(
alias: str,
field: Union[graphql.GraphQLField, graphql.GraphQLInputField],
) -> DataModelFieldBase:
final_data_type = DataType(is_optional=True)
final_data_type = DataType(
is_optional=True,
use_union_operator=self.use_union_operator,
use_standard_collections=self.use_standard_collections,
)
data_type = final_data_type
obj = field.type

while graphql.is_list_type(obj) or graphql.is_non_null_type(obj):
if graphql.is_list_type(obj):
data_type.is_list = True

new_data_type = DataType(is_optional=True)
new_data_type = DataType(
is_optional=True,
use_union_operator=self.use_union_operator,
use_standard_collections=self.use_standard_collections,
)
data_type.data_types = [new_data_type]

data_type = new_data_type
Expand Down
7 changes: 1 addition & 6 deletions datamodel_code_generator/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@

import pydantic
from packaging import version
from pydantic import (
StrictBool,
StrictInt,
StrictStr,
create_model,
)
from pydantic import StrictBool, StrictInt, StrictStr, create_model

from datamodel_code_generator.format import PythonVersion
from datamodel_code_generator.imports import (
Expand Down
28 changes: 28 additions & 0 deletions tests/data/expected/main/graphql/annotated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# generated by datamodel-codegen:
# filename: annotated.graphql
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import List, Optional, TypeAlias

from pydantic import BaseModel, Field
from typing_extensions import Annotated, Literal

Boolean: TypeAlias = bool
"""
The `Boolean` scalar type represents `true` or `false`.
"""


String: TypeAlias = str
"""
The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
"""


class A(BaseModel):
field: String
listField: List[String]
listListField: List[List[String]]
typename__: Annotated[Optional[Literal['A']], Field(alias='__typename')] = 'A'
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions tests/data/expected/main/graphql/use_standard_collections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# generated by datamodel-codegen:
# filename: use-standard-collections.graphql
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Optional, TypeAlias

from pydantic import BaseModel, Field
from typing_extensions import Literal

Boolean: TypeAlias = bool
"""
The `Boolean` scalar type represents `true` or `false`.
"""


String: TypeAlias = str
"""
The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
"""


class A(BaseModel):
field: String
listField: list[String]
listListField: list[list[String]]
typename__: Optional[Literal['A']] = Field('A', alias='__typename')
41 changes: 41 additions & 0 deletions tests/data/expected/main/graphql/use_union_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# generated by datamodel-codegen:
# filename: use-union-operator.graphql
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import List, TypeAlias

from pydantic import BaseModel, Field
from typing_extensions import Literal

Boolean: TypeAlias = bool
"""
The `Boolean` scalar type represents `true` or `false`.
"""


String: TypeAlias = str
"""
The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
"""


class A(BaseModel):
field: String
listField: List[String]
listListField: List[List[String]]
listListOptionalField: List[List[String | None]]
listOptionalField: List[String | None]
listOptionalListField: List[List[String] | None]
listOptionalListOptionalField: List[List[String | None] | None]
optionalField: String | None = None
optionalListListField: List[List[String]] | None = Field(default_factory=list)
optionalListListOptionalField: List[List[String | None]] | None = Field(
default_factory=list
)
optionalListOptionalField: List[String | None] | None = Field(default_factory=list)
optionalListOptionalListField: List[List[String] | None] | None = Field(
default_factory=list
)
typename__: Literal['A'] | None = Field('A', alias='__typename')
File renamed without changes.
23 changes: 23 additions & 0 deletions tests/data/expected/main/jsonschema/custom_formatters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# generated by datamodel-codegen:
# filename: person.json
# timestamp: 2019-07-26T00:00:00+00:00

# MIT License
#
# Copyright (c) 2023 Blah-blah
#
from __future__ import annotations

from typing import List, Optional

from pydantic import BaseModel, Field, conint


class Person(BaseModel):
firstName: Optional[str] = Field(None, description="The person's first name.")
lastName: Optional[str] = Field(None, description="The person's last name.")
age: Optional[conint(ge=0)] = Field(
None, description='Age in years which must be equal to or greater than zero.'
)
friends: Optional[List] = None
comment: None = None
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit e4fa23a

Please sign in to comment.