|
| 1 | +# Base Options |
| 2 | + |
| 3 | +## Options |
| 4 | + |
| 5 | +| Option | Description | |
| 6 | +|--------|-------------| |
| 7 | +| [`--encoding`](#encoding) | Specify character encoding for input and output files. | |
| 8 | +| [`--input`](#input) | Specify the input schema file path. | |
| 9 | +| [`--input-file-type`](#input-file-type) | Specify the input file type for code generation. | |
| 10 | +| [`--output`](#output) | Specify the destination path for generated Python code. | |
| 11 | +| [`--url`](#url) | Fetch schema from URL with custom HTTP headers. | |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## `--encoding` {#encoding} |
| 16 | + |
| 17 | +Specify character encoding for input and output files. |
| 18 | + |
| 19 | +The `--encoding` flag sets the character encoding used when reading |
| 20 | +the schema file and writing the generated Python code. This is useful |
| 21 | +for schemas containing non-ASCII characters (e.g., Japanese, Chinese). |
| 22 | +Default is utf-8. |
| 23 | + |
| 24 | +!!! tip "Usage" |
| 25 | + |
| 26 | + ```bash |
| 27 | + datamodel-codegen --input schema.json --encoding utf-8 # (1)! |
| 28 | + ``` |
| 29 | + |
| 30 | + 1. :material-arrow-left: `--encoding` - the option documented here |
| 31 | + |
| 32 | +??? example "Input Schema" |
| 33 | + |
| 34 | + ```json |
| 35 | + { |
| 36 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 37 | + "title": "日本語Model", |
| 38 | + "description": "モデルの説明文", |
| 39 | + "type": "object", |
| 40 | + "properties": { |
| 41 | + "名前": { |
| 42 | + "type": "string", |
| 43 | + "description": "ユーザー名" |
| 44 | + }, |
| 45 | + "年齢": { |
| 46 | + "type": "integer" |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + ``` |
| 51 | + |
| 52 | +??? example "Output" |
| 53 | + |
| 54 | + ```python |
| 55 | + # generated by datamodel-codegen: |
| 56 | + # filename: encoding_test.json |
| 57 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 58 | + |
| 59 | + from __future__ import annotations |
| 60 | + |
| 61 | + from typing import Optional |
| 62 | + |
| 63 | + from pydantic import BaseModel, Field |
| 64 | + |
| 65 | + |
| 66 | + class 日本語Model(BaseModel): |
| 67 | + 名前: Optional[str] = Field(None, description='ユーザー名') |
| 68 | + 年齢: Optional[int] = None |
| 69 | + ``` |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## `--input` {#input} |
| 74 | + |
| 75 | +Specify the input schema file path. |
| 76 | + |
| 77 | +The `--input` flag specifies the path to the schema file (JSON Schema, |
| 78 | +OpenAPI, GraphQL, etc.). Multiple input files can be specified to merge |
| 79 | +schemas. Required unless using `--url` to fetch schema from a URL. |
| 80 | + |
| 81 | +!!! tip "Usage" |
| 82 | + |
| 83 | + ```bash |
| 84 | + datamodel-codegen --input schema.json --input pet_simple.json --output output.py # (1)! |
| 85 | + ``` |
| 86 | + |
| 87 | + 1. :material-arrow-left: `--input` - the option documented here |
| 88 | + |
| 89 | +??? example "Input Schema" |
| 90 | + |
| 91 | + ```json |
| 92 | + { |
| 93 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 94 | + "title": "Pet", |
| 95 | + "type": "object", |
| 96 | + "properties": { |
| 97 | + "id": { |
| 98 | + "type": "integer" |
| 99 | + }, |
| 100 | + "name": { |
| 101 | + "type": "string" |
| 102 | + }, |
| 103 | + "tag": { |
| 104 | + "type": "string" |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + ``` |
| 109 | + |
| 110 | +??? example "Output" |
| 111 | + |
| 112 | + ```python |
| 113 | + # generated by datamodel-codegen: |
| 114 | + # filename: pet_simple.json |
| 115 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 116 | + |
| 117 | + from __future__ import annotations |
| 118 | + |
| 119 | + from typing import Optional |
| 120 | + |
| 121 | + from pydantic import BaseModel |
| 122 | + |
| 123 | + |
| 124 | + class Pet(BaseModel): |
| 125 | + id: Optional[int] = None |
| 126 | + name: Optional[str] = None |
| 127 | + tag: Optional[str] = None |
| 128 | + ``` |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +## `--input-file-type` {#input-file-type} |
| 133 | + |
| 134 | +Specify the input file type for code generation. |
| 135 | + |
| 136 | +The `--input-file-type` flag explicitly sets the input format when it cannot |
| 137 | +be auto-detected from the file extension. Supported types: openapi, jsonschema, |
| 138 | +json, yaml, csv, graphql. |
| 139 | + |
| 140 | +!!! tip "Usage" |
| 141 | + |
| 142 | + ```bash |
| 143 | + datamodel-codegen --input schema.json --input-file-type json # (1)! |
| 144 | + ``` |
| 145 | + |
| 146 | + 1. :material-arrow-left: `--input-file-type` - the option documented here |
| 147 | + |
| 148 | +??? example "Input Schema" |
| 149 | + |
| 150 | + ```json |
| 151 | + { |
| 152 | + "Pet": { |
| 153 | + "name": "dog", |
| 154 | + "age": 2 |
| 155 | + } |
| 156 | + } |
| 157 | + ``` |
| 158 | + |
| 159 | +??? example "Output" |
| 160 | + |
| 161 | + ```python |
| 162 | + # generated by datamodel-codegen: |
| 163 | + # filename: pet.json |
| 164 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 165 | + |
| 166 | + from __future__ import annotations |
| 167 | + |
| 168 | + from pydantic import BaseModel |
| 169 | + |
| 170 | + |
| 171 | + class Pet(BaseModel): |
| 172 | + name: str |
| 173 | + age: int |
| 174 | + |
| 175 | + |
| 176 | + class Model(BaseModel): |
| 177 | + Pet: Pet |
| 178 | + ``` |
| 179 | + |
| 180 | +--- |
| 181 | + |
| 182 | +## `--output` {#output} |
| 183 | + |
| 184 | +Specify the destination path for generated Python code. |
| 185 | + |
| 186 | +The `--output` flag specifies where to write the generated Python code. |
| 187 | +It can be either a file path (single-file output) or a directory path |
| 188 | +(multi-file output for modular schemas). If omitted, the generated code |
| 189 | +is written to stdout. |
| 190 | + |
| 191 | +!!! tip "Usage" |
| 192 | + |
| 193 | + ```bash |
| 194 | + datamodel-codegen --input schema.json --input pet_simple.json --output output.py # (1)! |
| 195 | + ``` |
| 196 | + |
| 197 | + 1. :material-arrow-left: `--output` - the option documented here |
| 198 | + |
| 199 | +??? example "Input Schema" |
| 200 | + |
| 201 | + ```json |
| 202 | + { |
| 203 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 204 | + "title": "Pet", |
| 205 | + "type": "object", |
| 206 | + "properties": { |
| 207 | + "id": { |
| 208 | + "type": "integer" |
| 209 | + }, |
| 210 | + "name": { |
| 211 | + "type": "string" |
| 212 | + }, |
| 213 | + "tag": { |
| 214 | + "type": "string" |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + ``` |
| 219 | + |
| 220 | +??? example "Output" |
| 221 | + |
| 222 | + ```python |
| 223 | + # generated by datamodel-codegen: |
| 224 | + # filename: pet_simple.json |
| 225 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 226 | + |
| 227 | + from __future__ import annotations |
| 228 | + |
| 229 | + from typing import Optional |
| 230 | + |
| 231 | + from pydantic import BaseModel |
| 232 | + |
| 233 | + |
| 234 | + class Pet(BaseModel): |
| 235 | + id: Optional[int] = None |
| 236 | + name: Optional[str] = None |
| 237 | + tag: Optional[str] = None |
| 238 | + ``` |
| 239 | + |
| 240 | +--- |
| 241 | + |
| 242 | +## `--url` {#url} |
| 243 | + |
| 244 | +Fetch schema from URL with custom HTTP headers. |
| 245 | + |
| 246 | +The `--url` flag specifies a remote URL to fetch the schema from instead of |
| 247 | +a local file. The `--http-headers` flag adds custom HTTP headers to the request, |
| 248 | +useful for authentication (e.g., Bearer tokens) or custom API requirements. |
| 249 | +Format: `HeaderName:HeaderValue`. |
| 250 | + |
| 251 | +!!! tip "Usage" |
| 252 | + |
| 253 | + ```bash |
| 254 | + datamodel-codegen --input schema.json --url https://api.example.com/schema.json --http-headers "Authorization:Bearer token" # (1)! |
| 255 | + ``` |
| 256 | + |
| 257 | + 1. :material-arrow-left: `--url` - the option documented here |
| 258 | + |
| 259 | +??? example "Input Schema" |
| 260 | + |
| 261 | + ```json |
| 262 | + { |
| 263 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 264 | + "title": "Pet", |
| 265 | + "type": "object", |
| 266 | + "properties": { |
| 267 | + "id": { |
| 268 | + "type": "integer" |
| 269 | + }, |
| 270 | + "name": { |
| 271 | + "type": "string" |
| 272 | + }, |
| 273 | + "tag": { |
| 274 | + "type": "string" |
| 275 | + } |
| 276 | + } |
| 277 | + } |
| 278 | + ``` |
| 279 | + |
| 280 | +??? example "Output" |
| 281 | + |
| 282 | + ```python |
| 283 | + # generated by datamodel-codegen: |
| 284 | + # filename: https://api.example.com/schema.json |
| 285 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 286 | + |
| 287 | + from __future__ import annotations |
| 288 | + |
| 289 | + from typing import Optional |
| 290 | + |
| 291 | + from pydantic import BaseModel |
| 292 | + |
| 293 | + |
| 294 | + class Pet(BaseModel): |
| 295 | + id: Optional[int] = None |
| 296 | + name: Optional[str] = None |
| 297 | + tag: Optional[str] = None |
| 298 | + ``` |
| 299 | + |
| 300 | +--- |
| 301 | + |
0 commit comments