@@ -73,6 +73,25 @@ def _show_version(value: bool) -> None:
7373 raise typer .Exit ()
7474
7575
76+ def _resolve_remote_reference_options (
77+ allow_remote_refs : Optional [bool ], allow_private_network : bool
78+ ) -> tuple [Optional [bool ], bool ]:
79+ match allow_remote_refs , allow_private_network :
80+ case False , True :
81+ return False , False
82+ case None , True :
83+ return True , True
84+ case _:
85+ pass
86+ return allow_remote_refs , allow_private_network
87+
88+
89+ def _parse_specified_tags (specify_tags : Optional [str ]) -> set [str ]:
90+ if not specify_tags :
91+ return set ()
92+ return {tag for raw_tag in specify_tags .split ("," ) if (tag := raw_tag .strip ())}
93+
94+
7695@lru_cache (maxsize = 1 )
7796def _get_command () -> Command :
7897 return get_command (app )
@@ -108,6 +127,22 @@ def main(
108127 "present."
109128 ),
110129 ),
130+ allow_remote_refs : Optional [bool ] = typer .Option (
131+ None ,
132+ "--allow-remote-refs/--no-allow-remote-refs" ,
133+ help = (
134+ "Allow or block fetching remote $ref targets over HTTP/HTTPS. "
135+ "The default follows datamodel-code-generator compatibility behavior."
136+ ),
137+ ),
138+ allow_private_network : bool = typer .Option (
139+ False ,
140+ "--allow-private-network" ,
141+ help = (
142+ "Allow trusted remote $ref targets on local or private network "
143+ "addresses."
144+ ),
145+ ),
111146 output_model_type : DataModelType = typer .Option (
112147 DataModelType .PydanticV2BaseModel .value , "--output-model-type" , "-d"
113148 ),
@@ -162,6 +197,8 @@ def main(
162197 disable_timestamp = disable_timestamp ,
163198 strict_nullable = strict_nullable ,
164199 include_request_argument = include_request_argument ,
200+ allow_remote_refs = allow_remote_refs ,
201+ allow_private_network = allow_private_network ,
165202 generate_routers = generate_routers ,
166203 specify_tags = specify_tags ,
167204 output_model_type = output_model_type ,
@@ -203,6 +240,8 @@ def generate_code(
203240 disable_timestamp : bool = False ,
204241 strict_nullable : bool = False ,
205242 include_request_argument : bool = False ,
243+ allow_remote_refs : Optional [bool ] = None ,
244+ allow_private_network : bool = False ,
206245 generate_routers : Optional [bool ] = None ,
207246 specify_tags : Optional [str ] = None ,
208247 output_model_type : DataModelType = DataModelType .PydanticV2BaseModel ,
@@ -226,6 +265,9 @@ def generate_code(
226265 custom_visitors = []
227266 data_model_types = get_data_model_types (output_model_type , python_version )
228267 code_formatter = _get_code_formatter (python_version , Path ().resolve ())
268+ allow_remote_refs , allow_private_network = _resolve_remote_reference_options (
269+ allow_remote_refs , allow_private_network
270+ )
229271
230272 parser = OpenAPIParser (
231273 input_text ,
@@ -239,6 +281,8 @@ def generate_code(
239281 target_python_version = python_version ,
240282 strict_nullable = strict_nullable ,
241283 include_request_argument = include_request_argument ,
284+ allow_remote_refs = allow_remote_refs ,
285+ allow_private_network = allow_private_network ,
242286 use_annotated = use_annotated ,
243287 reuse_model = reuse_model ,
244288 enable_faux_immutability = enable_faux_immutability ,
@@ -304,9 +348,7 @@ def generate_code(
304348 specified_tags = set ()
305349 existing_main_has_router_includes = False
306350 if generate_routers and specify_tags :
307- specified_tags = {
308- tag .strip () for tag in str (specify_tags ).split ("," ) if tag .strip ()
309- }
351+ specified_tags = _parse_specified_tags (specify_tags )
310352 main_path = output_dir / "main.py"
311353 if main_path .exists ():
312354 existing_main_has_router_includes = (
0 commit comments