Skip to content

Commit 365419e

Browse files
authored
Fix title ignored when creating enums from merging allOf's or anyOf's objects (#2975)
* Fix x_enum_varnames ignored because class variable name was used insteaad of its alias * Keep existing behavior when title isn't defined and the enum's type can be inferred and used as name * Improve fix for unveiled incorrect behavior --------- Co-authored-by: Antonio Spadaro <9268789+ilovelinux@users.noreply.github.com>
1 parent 4661406 commit 365419e

2 files changed

Lines changed: 4 additions & 7 deletions

File tree

src/datamodel_code_generator/parser/jsonschema.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,6 @@ def _extract_const_enum_from_combined( # noqa: PLR0912
854854

855855
if item.title:
856856
varnames.append(item.title)
857-
else:
858-
varnames.append(str(const_value))
859857

860858
if inferred_type is None and const_value is not None:
861859
match const_value:
@@ -902,8 +900,7 @@ def _create_synthetic_enum_obj(
902900
enum=final_enum,
903901
title=original.title,
904902
description=original.description,
905-
x_enum_varnames=final_varnames,
906-
**({"default": original.default} if original.has_default else {}),
903+
**({"x-enum-varnames": final_varnames} | ({"default": original.default} if original.has_default else {})),
907904
)
908905

909906
def is_constraints_field(self, obj: JsonSchemaObject) -> bool:

tests/data/expected/main/jsonschema/oneof_const_enum_int.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88

99

1010
class StatusCode(IntEnum):
11-
integer_200 = 200
12-
integer_404 = 404
13-
integer_500 = 500
11+
OK = 200
12+
Not_Found = 404
13+
Server_Error = 500

0 commit comments

Comments
 (0)