Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/datamodel_code_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,10 +965,7 @@ def load_mcp_tools_data() -> Any:
from datamodel_code_generator.config import AvroParserConfig # noqa: PLC0415
from datamodel_code_generator.parser.avro import AvroParser # noqa: PLC0415

avro_additional_options: AvroParserConfigDict = {
"apply_default_values_for_required_fields": True,
**additional_options,
}
avro_additional_options: AvroParserConfigDict = {**additional_options}
parser_config = _create_parser_config(AvroParserConfig, config, avro_additional_options)
parser = AvroParser(source=source, config=parser_config) # ty: ignore
elif input_file_type == InputFileType.GraphQL:
Expand Down
28 changes: 3 additions & 25 deletions src/datamodel_code_generator/parser/asyncapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import re
from contextlib import contextmanager
from dataclasses import dataclass, replace
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING, Any, ClassVar

Expand Down Expand Up @@ -105,7 +105,6 @@ class AsyncAPISchema:
path: list[str]
context: AsyncAPIContext
parse_as_file: bool = False
apply_avro_default_values: bool = False


class MultiFormatSchemaObject(BaseModel):
Expand Down Expand Up @@ -291,19 +290,6 @@ def _schema(
parse_as_file=parse_as_file,
)

@contextmanager
def _avro_default_values(self, *, enabled: bool) -> Iterator[None]:
if not enabled:
yield
return

previous = self.apply_default_values_for_required_fields
self.apply_default_values_for_required_fields = True
try:
yield
finally:
self.apply_default_values_for_required_fields = previous

def _schema_context_for_converted_schema(
self,
converted_schema: dict[str, YamlValue],
Expand Down Expand Up @@ -378,12 +364,7 @@ def _iter_schema_format_schemas(
converted_schema = convert_avro_schema_data(raw_schema)
converted_schema.setdefault("title", name)
context = self._schema_context_for_converted_schema(converted_schema, path)
return [
replace(
self._schema(name, converted_schema, context.root_parts, context, parse_as_file=True),
apply_avro_default_values=True,
)
]
return [self._schema(name, converted_schema, context.root_parts, context, parse_as_file=True)]
case "protobuf":
context = self._current_asyncapi_context()
return self._iter_converted_schema_schemas(
Expand Down Expand Up @@ -1004,10 +985,7 @@ def parse_raw(self) -> None:
if path_key in parsed_paths:
continue
parsed_paths.add(path_key)
with (
self._asyncapi_context(schema.context),
self._avro_default_values(enabled=schema.apply_avro_default_values),
):
with self._asyncapi_context(schema.context):
if schema.parse_as_file:
self._parse_file(raw_schema, schema.name, schema.path)
else:
Expand Down
4 changes: 0 additions & 4 deletions src/datamodel_code_generator/parser/avro.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,6 @@ def _convert_record(self, schema: JsonSchema, fullname: str) -> JsonSchema:
field_schema["x-avro-aliases"] = field["aliases"]
if "order" in field:
field_schema["x-avro-order"] = field["order"]
if "default" in field:
field_schema["default"] = field["default"]
required.append(field_name)
properties[field_name] = field_schema

Expand Down Expand Up @@ -452,8 +450,6 @@ def _convert_enum(self, schema: JsonSchema, fullname: str) -> JsonSchema:
seen_symbols.add(symbol)
converted: JsonSchema = {"type": "string", "enum": symbols}
self._copy_common_metadata(schema, converted)
if "default" in schema:
converted["default"] = schema["default"]
converted["x-avro-fullname"] = fullname
return converted

Expand Down
4 changes: 2 additions & 2 deletions tests/data/expected/main/asyncapi/multi_format_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class UserStatus(BaseModel):

class UserSignupUserSignupPayload(BaseModel):
displayName: str
age: int | None = None
signupCount: int = 0
age: int | None
signupCount: int


class UserSignupUserSignupHeaders(BaseModel):
Expand Down
16 changes: 8 additions & 8 deletions tests/data/expected/main/avro/py310/constructs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Status(Enum):

class Address(BaseModel):
street: str
zip: str | None = None
zip: str | None


class MD5(RootModel[conbytes(min_length=16, max_length=16)]):
Expand All @@ -43,17 +43,17 @@ class User(BaseModel):
"""
Stable identifier
"""
active: bool = True
age: int = 0
active: bool
age: int
visits: int
score: float
rating: float
payload: bytes
status: Status = 'ACTIVE'
tags: list[str] = []
attributes: dict[str, str] = {}
status: Status
tags: list[str]
attributes: dict[str, str]
address: Address
previous: User | None = None
previous: User | None
hash: MD5
traceId: TraceId
price: Decimal
Expand All @@ -69,7 +69,7 @@ class User(BaseModel):
localUpdatedAt: NaiveDatetime
localProcessedAt: NaiveDatetime
duration: Duration
choice: str | int | None = None
choice: str | int | None
rawDecimalText: str


Expand Down
2 changes: 1 addition & 1 deletion tests/data/expected/main/avro/py310/official_long_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class LongList(BaseModel):
value: int
next: LongList | None = None
next: LongList | None


LongList.model_rebuild()
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ class Person(BaseModel):
height: int
weight: int
name: str
hacker: bool = False
hacker: bool
30 changes: 15 additions & 15 deletions tests/data/expected/main/avro/py310/spec_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class DefaultRecord(BaseModel):
value: int = 1
value: int


class DefaultEnum(Enum):
Expand Down Expand Up @@ -75,22 +75,22 @@ class Array(BaseModel):


class SpecMatrix(BaseModel):
nullDefault: None = None
booleanDefault: bool = False
intDefault: int = 1
longDefault: int = 2
floatDefault: float = 1.5
doubleDefault: float = 2.5
bytesDefault: bytes = 'ÿ'
stringDefault: str = 'foo'
recordDefault: DefaultRecord = Field({'value': 1}, validate_default=True)
enumDefault: DefaultEnum = 'FOO'
arrayDefault: list[int] = [1]
mapDefault: dict[str, int] = {'a': 1}
fixedDefault: DefaultFixed = Field('ÿ', validate_default=True)
nullDefault: None
booleanDefault: bool
intDefault: int
longDefault: int
floatDefault: float
doubleDefault: float
bytesDefault: bytes
stringDefault: str
recordDefault: DefaultRecord
enumDefault: DefaultEnum
arrayDefault: list[int]
mapDefault: dict[str, int]
fixedDefault: DefaultFixed
ascendingOrder: str
descendingOrder: str
nullableAfterValue: str | None = 'value'
nullableAfterValue: str | None
sameKindNamedUnion: RA | RB | EA | EB | FA | FB
arrayOfRecords: list[ArrayItem]
mapOfEnums: dict[str, MapValue]
Expand Down
16 changes: 8 additions & 8 deletions tests/data/expected/main/avro/py311/constructs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Status(StrEnum):

class Address(BaseModel):
street: str
zip: str | None = None
zip: str | None


class MD5(RootModel[conbytes(min_length=16, max_length=16)]):
Expand All @@ -43,17 +43,17 @@ class User(BaseModel):
"""
Stable identifier
"""
active: bool = True
age: int = 0
active: bool
age: int
visits: int
score: float
rating: float
payload: bytes
status: Status = 'ACTIVE'
tags: list[str] = []
attributes: dict[str, str] = {}
status: Status
tags: list[str]
attributes: dict[str, str]
address: Address
previous: User | None = None
previous: User | None
hash: MD5
traceId: TraceId
price: Decimal
Expand All @@ -69,7 +69,7 @@ class User(BaseModel):
localUpdatedAt: NaiveDatetime
localProcessedAt: NaiveDatetime
duration: Duration
choice: str | int | None = None
choice: str | int | None
rawDecimalText: str


Expand Down
2 changes: 1 addition & 1 deletion tests/data/expected/main/avro/py311/official_long_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class LongList(BaseModel):
value: int
next: LongList | None = None
next: LongList | None


LongList.model_rebuild()
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ class Person(BaseModel):
height: int
weight: int
name: str
hacker: bool = False
hacker: bool
30 changes: 15 additions & 15 deletions tests/data/expected/main/avro/py311/spec_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class DefaultRecord(BaseModel):
value: int = 1
value: int


class DefaultEnum(StrEnum):
Expand Down Expand Up @@ -75,22 +75,22 @@ class Array(BaseModel):


class SpecMatrix(BaseModel):
nullDefault: None = None
booleanDefault: bool = False
intDefault: int = 1
longDefault: int = 2
floatDefault: float = 1.5
doubleDefault: float = 2.5
bytesDefault: bytes = 'ÿ'
stringDefault: str = 'foo'
recordDefault: DefaultRecord = Field({'value': 1}, validate_default=True)
enumDefault: DefaultEnum = 'FOO'
arrayDefault: list[int] = [1]
mapDefault: dict[str, int] = {'a': 1}
fixedDefault: DefaultFixed = Field('ÿ', validate_default=True)
nullDefault: None
booleanDefault: bool
intDefault: int
longDefault: int
floatDefault: float
doubleDefault: float
bytesDefault: bytes
stringDefault: str
recordDefault: DefaultRecord
enumDefault: DefaultEnum
arrayDefault: list[int]
mapDefault: dict[str, int]
fixedDefault: DefaultFixed
ascendingOrder: str
descendingOrder: str
nullableAfterValue: str | None = 'value'
nullableAfterValue: str | None
sameKindNamedUnion: RA | RB | EA | EB | FA | FB
arrayOfRecords: list[ArrayItem]
mapOfEnums: dict[str, MapValue]
Expand Down
16 changes: 8 additions & 8 deletions tests/data/expected/main/avro/py312/constructs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Status(StrEnum):

class Address(BaseModel):
street: str
zip: str | None = None
zip: str | None


class MD5(RootModel[conbytes(min_length=16, max_length=16)]):
Expand All @@ -43,17 +43,17 @@ class User(BaseModel):
"""
Stable identifier
"""
active: bool = True
age: int = 0
active: bool
age: int
visits: int
score: float
rating: float
payload: bytes
status: Status = 'ACTIVE'
tags: list[str] = []
attributes: dict[str, str] = {}
status: Status
tags: list[str]
attributes: dict[str, str]
address: Address
previous: User | None = None
previous: User | None
hash: MD5
traceId: TraceId
price: Decimal
Expand All @@ -69,7 +69,7 @@ class User(BaseModel):
localUpdatedAt: NaiveDatetime
localProcessedAt: NaiveDatetime
duration: Duration
choice: str | int | None = None
choice: str | int | None
rawDecimalText: str


Expand Down
2 changes: 1 addition & 1 deletion tests/data/expected/main/avro/py312/official_long_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class LongList(BaseModel):
value: int
next: LongList | None = None
next: LongList | None


LongList.model_rebuild()
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ class Person(BaseModel):
height: int
weight: int
name: str
hacker: bool = False
hacker: bool
Loading
Loading