Skip to content

Commit 5ffc2a1

Browse files
Merge pull request #4 from rbeard0330/master
Fix unsafe Config.extra access & add directive to suppress ESLint
2 parents 54f9fee + ae98251 commit 5ffc2a1

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

pydantic2ts/cli/script.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,28 @@ def generate_json_schema(models: List[Type[BaseModel]]) -> str:
141141
'[k: string]: any' from being added to every interface. This change is reverted
142142
once the schema has been generated.
143143
"""
144-
model_extras = [m.Config.extra for m in models]
144+
model_extras = [getattr(m.Config, 'extra', None) for m in models]
145145

146-
for m in models:
147-
if m.Config.extra != Extra.allow:
148-
m.Config.extra = Extra.forbid
146+
try:
147+
for m in models:
148+
if getattr(m.Config, 'extra', None) != Extra.allow:
149+
m.Config.extra = Extra.forbid
149150

150-
master_model = create_model("_Master_", **{m.__name__: (m, ...) for m in models})
151-
master_model.Config.extra = Extra.forbid
152-
master_model.Config.schema_extra = staticmethod(clean_schema)
151+
master_model = create_model("_Master_", **{m.__name__: (m, ...) for m in models})
152+
master_model.Config.extra = Extra.forbid
153+
master_model.Config.schema_extra = staticmethod(clean_schema)
153154

154-
schema = json.loads(master_model.schema_json())
155+
schema = json.loads(master_model.schema_json())
155156

156-
for d in schema.get("definitions", {}).values():
157-
clean_schema(d)
157+
for d in schema.get("definitions", {}).values():
158+
clean_schema(d)
158159

159-
for m, x in zip(models, model_extras):
160-
m.Config.extra = x
160+
return json.dumps(schema, indent=2)
161161

162-
return json.dumps(schema, indent=2)
162+
finally:
163+
for m, x in zip(models, model_extras):
164+
if x is not None:
165+
m.Config.extra = x
163166

164167

165168
def generate_typescript_defs(
@@ -196,6 +199,7 @@ def generate_typescript_defs(
196199
banner_comment = "\n".join(
197200
[
198201
"/* tslint:disable */",
202+
"/* eslint-disable */",
199203
"/**",
200204
"/* This file was automatically generated from pydantic models by running pydantic2ts.",
201205
"/* Do not modify it by hand - just update the pydantic models and then re-run the script",

0 commit comments

Comments
 (0)