Skip to content

Commit e0eb454

Browse files
authored
Merge pull request #43 from bogdandm/fix-42
Fix invalid chars in Literal value
2 parents 21a4eb5 + fe0441e commit e0eb454

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

json_to_models/dynamic_typing/complex.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from functools import partial
23
from itertools import chain
34
from typing import AbstractSet, Dict, Iterable, List, Optional, Tuple, Type, Union
@@ -266,7 +267,7 @@ def to_typing_code(self, types_style: Dict[Union['BaseType', Type['BaseType']],
266267
limit = options.get(self.TypeStyle.max_literals)
267268
if limit is None or len(self.literals) < limit:
268269
parts = ', '.join(
269-
'"{}"'.format(s.replace('\\', '\\\\').replace('"', '\\"'))
270+
json.dumps(s)
270271
for s in sorted(self.literals)
271272
)
272273
return [(Literal.__module__, 'Literal')], f"Literal[{parts}]"

test/test_code_generation/test_models_code_generator.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,21 @@
33
import pytest
44
from typing_extensions import Literal
55

6-
from json_to_models.dynamic_typing import (AbsoluteModelRef, BaseType, DDict, DList, DOptional, IntString, IsoDateString, ModelMeta, ModelPtr, StringLiteral, StringSerializable, Unknown, compile_imports)
6+
from json_to_models.dynamic_typing import (
7+
AbsoluteModelRef,
8+
BaseType,
9+
DDict,
10+
DList,
11+
DOptional,
12+
IntString,
13+
IsoDateString,
14+
ModelMeta,
15+
ModelPtr,
16+
StringLiteral,
17+
StringSerializable,
18+
Unknown,
19+
compile_imports,
20+
)
721
from json_to_models.models.base import GenericModelCodeGenerator, generate_code
822
from json_to_models.models.structure import sort_fields
923
from json_to_models.models.utils import indent
@@ -146,6 +160,39 @@ class Test:
146160
d: Dict[str, Any]
147161
baz: Optional[List[List[str]]]
148162
""")
163+
},
164+
"literals": {
165+
"model": ("Test", {
166+
"a": StringLiteral({'basic'}),
167+
"b": StringLiteral({'with space'}),
168+
"c": StringLiteral({'with\ttab'}),
169+
"d": StringLiteral({'with\nnew_line'}),
170+
"e": StringLiteral({'with \'"qoutes"\''}),
171+
"f": StringLiteral({'with \\ // slash'}),
172+
}),
173+
"fields": {
174+
"imports": f"{LITERAL_SOURCE} import Literal",
175+
"fields": [
176+
'a: Literal["basic"]',
177+
'b: Literal["with space"]',
178+
'c: Literal["with\\ttab"]',
179+
'd: Literal["with\\nnew_line"]',
180+
'e: Literal["with \'\\"qoutes\\"\'"]',
181+
'f: Literal["with \\\\ // slash"]'
182+
]
183+
},
184+
"generated": trim(f"""
185+
{LITERAL_SOURCE} import Literal
186+
187+
188+
class Test:
189+
a: Literal["basic"]
190+
b: Literal["with space"]
191+
c: Literal["with\\ttab"]
192+
d: Literal["with\\nnew_line"]
193+
e: Literal["with \'\\"qoutes\\"\'"]
194+
f: Literal["with \\\\ // slash"]
195+
""")
149196
}
150197
}
151198

0 commit comments

Comments
 (0)