Commit f6d4cbd
Add --allow-remote-refs to disable HTTP fetching of $ref by default (#3051)
* Add --allow-remote-refs flag to gate HTTP fetching of $ref targets
Remote $ref fetching over HTTP/HTTPS is now disabled by default.
When a $ref resolves to an HTTP(S) URL and --allow-remote-refs is not set,
a clear error message is shown instead of silently fetching remote content.
file:// URLs are still allowed without the flag since they are local.
--url input implicitly enables --allow-remote-refs.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Improve error message for missing local $ref files
When a $ref points to a local file that doesn't exist, the error now
clearly states "$ref file not found: <path>" instead of raising a raw
FileNotFoundError.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Validate HTTP responses before parsing as schema content
Check status codes and Content-Type headers when fetching remote $ref
targets over HTTP. Returns clear error messages for HTTP errors (4xx/5xx)
and unexpected HTML responses instead of cryptic YAML parse errors.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Remove redundant local `import json` in test functions
The module-level import already covers these usages.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Add @pytest.mark.cli_doc marker for --allow-remote-refs
CLI reference docs will be auto-generated by CI from this marker.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Set status_code and headers on httpx mock responses
Remove isinstance/hasattr guards from http.get_body() and instead
fix all test mocks to set status_code=200 and headers={}, matching
real httpx.Response behavior.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Use SchemaFetchError instead of bare Exception in HTTP responses
Add SchemaFetchError(Error) for HTTP error status codes and unexpected
HTML responses. This ensures errors are caught by the existing
`except Error` handler in __main__.py and shown as clean messages
instead of unhandled tracebacks.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Wrap transport errors and normalize Content-Type check
Catch httpx transport exceptions (DNS, timeout, connection errors) and
wrap them in SchemaFetchError. Normalize Content-Type to lowercase
before checking for text/html.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Harden test assertions for blocked and missing refs
Assert httpx.get is not called when remote refs are blocked, preventing
real HTTP leaks if the gate regresses. Assert the specific file path
in the missing local ref error, not just the generic prefix.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Change --allow-remote-refs default to warn instead of block
Per maintainer feedback, keep backward compatibility by allowing remote
$ref fetching by default but emit a FutureWarning when it happens
without explicit --allow-remote-refs. The flag becomes a three-state:
True (explicit opt-in, no warning), False (blocks), None (default,
allows with deprecation warning). The default will flip to False in
a future major version.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Support --no-allow-remote-refs via BooleanOptionalAction
Use BooleanOptionalAction (like --use-annotated) so users can
explicitly opt out with --no-allow-remote-refs from the CLI.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Add --no-allow-remote-refs to CLI_OPTION_META
BooleanOptionalAction registers both --allow-remote-refs and
--no-allow-remote-refs in argparse; the sync test requires both
to be in CLI_OPTION_META.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 7e1a5c7 commit f6d4cbd
19 files changed
Lines changed: 314 additions & 18 deletions
File tree
- src/datamodel_code_generator
- _types
- parser
- tests
- data/expected/main/input_model
- main
- jsonschema
- openapi
- parser
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
371 | 371 | | |
372 | 372 | | |
373 | 373 | | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
374 | 378 | | |
375 | 379 | | |
376 | 380 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
491 | 491 | | |
492 | 492 | | |
493 | 493 | | |
| 494 | + | |
494 | 495 | | |
495 | 496 | | |
496 | 497 | | |
| |||
934 | 935 | | |
935 | 936 | | |
936 | 937 | | |
| 938 | + | |
937 | 939 | | |
938 | 940 | | |
939 | 941 | | |
| |||
1080 | 1082 | | |
1081 | 1083 | | |
1082 | 1084 | | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
1083 | 1089 | | |
1084 | 1090 | | |
1085 | 1091 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| 119 | + | |
119 | 120 | | |
120 | 121 | | |
121 | 122 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
| 114 | + | |
114 | 115 | | |
115 | 116 | | |
116 | 117 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
128 | 138 | | |
129 | 139 | | |
130 | 140 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
265 | 265 | | |
266 | 266 | | |
267 | 267 | | |
| 268 | + | |
| 269 | + | |
268 | 270 | | |
269 | 271 | | |
270 | 272 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
| 144 | + | |
144 | 145 | | |
145 | 146 | | |
146 | 147 | | |
| |||
273 | 274 | | |
274 | 275 | | |
275 | 276 | | |
| 277 | + | |
276 | 278 | | |
277 | 279 | | |
278 | 280 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
| |||
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
46 | 62 | | |
47 | 63 | | |
48 | 64 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1101 | 1101 | | |
1102 | 1102 | | |
1103 | 1103 | | |
| 1104 | + | |
1104 | 1105 | | |
1105 | 1106 | | |
1106 | 1107 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| |||
3817 | 3818 | | |
3818 | 3819 | | |
3819 | 3820 | | |
| 3821 | + | |
| 3822 | + | |
| 3823 | + | |
| 3824 | + | |
| 3825 | + | |
| 3826 | + | |
| 3827 | + | |
| 3828 | + | |
| 3829 | + | |
| 3830 | + | |
| 3831 | + | |
| 3832 | + | |
| 3833 | + | |
| 3834 | + | |
| 3835 | + | |
| 3836 | + | |
| 3837 | + | |
3820 | 3838 | | |
3821 | 3839 | | |
3822 | 3840 | | |
| |||
3844 | 3862 | | |
3845 | 3863 | | |
3846 | 3864 | | |
3847 | | - | |
3848 | | - | |
3849 | | - | |
3850 | | - | |
| 3865 | + | |
| 3866 | + | |
| 3867 | + | |
| 3868 | + | |
| 3869 | + | |
| 3870 | + | |
| 3871 | + | |
| 3872 | + | |
3851 | 3873 | | |
3852 | 3874 | | |
3853 | 3875 | | |
| |||
0 commit comments