Skip to content

Commit

Permalink
Update datamodel-code-generator to 0.21.4
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi committed Aug 19, 2023
1 parent 07b70f9 commit 0ff596a
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 45 deletions.
41 changes: 35 additions & 6 deletions fastapi_code_generator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
LiteralType,
OpenAPIScope,
PythonVersion,
cached_property,
snooper_to_methods,
)
from datamodel_code_generator.imports import Import, Imports
from datamodel_code_generator.model import DataModel, DataModelFieldBase
from datamodel_code_generator.model import pydantic as pydantic_model
from datamodel_code_generator.model.pydantic import DataModelField
from datamodel_code_generator.model.pydantic import CustomRootType, DataModelField
from datamodel_code_generator.parser.jsonschema import JsonSchemaObject
from datamodel_code_generator.parser.openapi import MediaObject
from datamodel_code_generator.parser.openapi import OpenAPIParser as OpenAPIModelParser
Expand All @@ -43,6 +42,7 @@
ResponseObject,
)
from datamodel_code_generator.types import DataType, DataTypeManager, StrictTypes
from datamodel_code_generator.util import cached_property
from pydantic import BaseModel

RE_APPLICATION_JSON_PATTERN: Pattern[str] = re.compile(r'^application/.*json$')
Expand Down Expand Up @@ -245,16 +245,22 @@ def parse_info(self) -> Optional[Dict[str, Any]]:
result['servers'] = servers
return result or None

def parse_parameters(self, parameters: ParameterObject, path: List[str]) -> None:
super().parse_parameters(parameters, path)
self._temporary_operation['_parameters'].append(parameters)
def parse_all_parameters(
self,
name: str,
parameters: List[Union[ReferenceObject, ParameterObject]],
path: List[str],
) -> None:
super().parse_all_parameters(name, parameters, path)
self._temporary_operation['_parameters'].extend(parameters)

def get_parameter_type(
self,
parameters: ParameterObject,
parameters: Union[ReferenceObject, ParameterObject],
snake_case: bool,
path: List[str],
) -> Optional[Argument]:
parameters = self.resolve_object(parameters, ParameterObject)
orig_name = parameters.name
if snake_case:
name = stringcase.snakecase(parameters.name)
Expand All @@ -275,6 +281,7 @@ def get_parameter_type(
if not schema:
schema = parameters.schema_
data_type = self.parse_schema(name, schema, [*path, name])
data_type = self._collapse_root_model(data_type)
if not schema:
return None

Expand Down Expand Up @@ -357,6 +364,7 @@ def parse_request_body(
data_type = self.parse_schema(
name, media_obj.schema_, [*path, media_type]
)
data_type = self._collapse_root_model(data_type)
arguments.append(
# TODO: support multiple body
Argument(
Expand Down Expand Up @@ -413,6 +421,7 @@ def parse_responses(
if status_code_200:
data_type = list(status_code_200.values())[0]
if data_type:
data_type = self._collapse_root_model(data_type)
self.data_types.append(data_type)
else:
data_type = DataType(type='None')
Expand Down Expand Up @@ -462,3 +471,23 @@ def parse_operation(
path=f'/{path_name}', # type: ignore
method=method, # type: ignore
)

def _collapse_root_model(self, data_type: DataType) -> DataType:
reference = data_type.reference
import functools

if not (
reference
and (
len(reference.children) == 1
or functools.reduce(lambda a, b: a == b, reference.children)
)
):
return data_type
source = reference.source
if not isinstance(source, CustomRootType):
return data_type
data_type.remove_reference()
data_type = source.fields[0].data_type
self.results.remove(source)
return data_type
Loading

0 comments on commit 0ff596a

Please sign in to comment.