|
6 | 6 | |--------|-------------| |
7 | 7 | | [`--all-exports-collision-strategy`](#all-exports-collision-strategy) | Handle name collisions when exporting recursive module hiera... | |
8 | 8 | | [`--all-exports-scope`](#all-exports-scope) | Generate __all__ exports for child modules in __init__.py fi... | |
| 9 | +| [`--allow-remote-refs`](#allow-remote-refs) | Enable fetching of `$ref` targets over HTTP/HTTPS. | |
9 | 10 | | [`--check`](#check) | Verify generated code matches existing output without modify... | |
10 | 11 | | [`--disable-warnings`](#disable-warnings) | Suppress warning messages during code generation. | |
11 | 12 | | [`--generate-cli-command`](#generate-cli-command) | Generate CLI command from pyproject.toml configuration. | |
@@ -1161,6 +1162,94 @@ Use 'recursive' to include all descendant exports with collision handling. |
1161 | 1162 |
|
1162 | 1163 | --- |
1163 | 1164 |
|
| 1165 | +## `--allow-remote-refs` {#allow-remote-refs} |
| 1166 | + |
| 1167 | +Enable fetching of `$ref` targets over HTTP/HTTPS. |
| 1168 | + |
| 1169 | +When enabled, the generator will resolve `$ref` references that point to remote URLs, |
| 1170 | +including relative refs resolved against a schema's `$id` base URL. This is required |
| 1171 | +for schemas that reference definitions hosted on external servers. |
| 1172 | + |
| 1173 | +Automatically enabled when using `--url` input. |
| 1174 | + |
| 1175 | +!!! tip "Usage" |
| 1176 | + |
| 1177 | + ```bash |
| 1178 | + datamodel-codegen --input schema.json --allow-remote-refs # (1)! |
| 1179 | + ``` |
| 1180 | + |
| 1181 | + 1. :material-arrow-left: `--allow-remote-refs` - the option documented here |
| 1182 | + |
| 1183 | +??? example "Examples" |
| 1184 | + |
| 1185 | + **Input Schema:** |
| 1186 | + |
| 1187 | + ```json |
| 1188 | + { |
| 1189 | + "$id": "https://example.com/root_id.json", |
| 1190 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 1191 | + "definitions": { |
| 1192 | + "Person": { |
| 1193 | + "$ref": "person.json" |
| 1194 | + }, |
| 1195 | + "OriginalPerson": { |
| 1196 | + "$ref": "person.json" |
| 1197 | + }, |
| 1198 | + "Pet": { |
| 1199 | + "type": "object", |
| 1200 | + "properties": { |
| 1201 | + "name": { |
| 1202 | + "type": "string", |
| 1203 | + "examples": ["dog", "cat"] |
| 1204 | + }, |
| 1205 | + "owner": { |
| 1206 | + "$ref": "person.json" |
| 1207 | + } |
| 1208 | + } |
| 1209 | + } |
| 1210 | + } |
| 1211 | + } |
| 1212 | + ``` |
| 1213 | + |
| 1214 | + **Output:** |
| 1215 | + |
| 1216 | + ```python |
| 1217 | + # generated by datamodel-codegen: |
| 1218 | + # filename: root_id.json |
| 1219 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 1220 | + |
| 1221 | + from __future__ import annotations |
| 1222 | + |
| 1223 | + from typing import Any |
| 1224 | + |
| 1225 | + from pydantic import BaseModel, Field, RootModel, conint |
| 1226 | + |
| 1227 | + |
| 1228 | + class Model(RootModel[Any]): |
| 1229 | + root: Any |
| 1230 | + |
| 1231 | + |
| 1232 | + class Person(BaseModel): |
| 1233 | + firstName: str | None = Field(None, description="The person's first name.") |
| 1234 | + lastName: str | None = Field(None, description="The person's last name.") |
| 1235 | + age: conint(ge=0) | None = Field( |
| 1236 | + None, description='Age in years which must be equal to or greater than zero.' |
| 1237 | + ) |
| 1238 | + friends: list[Any] | None = None |
| 1239 | + comment: None = None |
| 1240 | + |
| 1241 | + |
| 1242 | + class OriginalPerson(RootModel[Person]): |
| 1243 | + root: Person |
| 1244 | + |
| 1245 | + |
| 1246 | + class Pet(BaseModel): |
| 1247 | + name: str | None = Field(None, examples=['dog', 'cat']) |
| 1248 | + owner: Person | None = None |
| 1249 | + ``` |
| 1250 | + |
| 1251 | +--- |
| 1252 | + |
1164 | 1253 | ## `--check` {#check} |
1165 | 1254 |
|
1166 | 1255 | Verify generated code matches existing output without modifying files. |
|
0 commit comments