Skip to content

Commit 24d7a35

Browse files
docs: update CLI reference documentation and prompt data
🤖 Generated by GitHub Actions
1 parent 7b7f1e9 commit 24d7a35

4 files changed

Lines changed: 83 additions & 1 deletion

File tree

docs/cli-reference/base-options.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| Option | Description |
66
|--------|-------------|
77
| [`--encoding`](#encoding) | Specify character encoding for input and output files. |
8+
| [`--external-ref-mapping`](#external-ref-mapping) | Map external `$ref` files to Python packages. |
89
| [`--input`](#input) | Specify the input schema file path. |
910
| [`--input-file-type`](#input-file-type) | Specify the input file type for code generation. |
1011
| [`--input-model`](#input-model) | Import a Python type or dict schema from a module. |
@@ -74,6 +75,83 @@ Default is UTF-8, which is the standard encoding for JSON and most modern text f
7475

7576
---
7677

78+
## `--external-ref-mapping` {#external-ref-mapping}
79+
80+
Map external `$ref` files to Python packages.
81+
82+
Use `--external-ref-mapping FILE_PATH=PYTHON_PACKAGE` to import referenced models from an existing package,
83+
instead of generating duplicate classes from external schema files.
84+
85+
!!! tip "Usage"
86+
87+
```bash
88+
datamodel-codegen --input schema.json --input-file-type openapi --external-ref-mapping common.yaml=mypackage.shared.models # (1)!
89+
```
90+
91+
1. :material-arrow-left: `--external-ref-mapping` - the option documented here
92+
93+
??? example "Examples"
94+
95+
**Input Schema:**
96+
97+
```yaml
98+
openapi: "3.0.3"
99+
info:
100+
title: API
101+
version: "1.0.0"
102+
paths: {}
103+
components:
104+
schemas:
105+
UserResponse:
106+
type: object
107+
properties:
108+
user:
109+
$ref: "common.yaml#/components/schemas/User"
110+
request_id:
111+
type: string
112+
required:
113+
- user
114+
- request_id
115+
ErrorResponse:
116+
type: object
117+
properties:
118+
error:
119+
$ref: "common.yaml#/components/schemas/Error"
120+
timestamp:
121+
type: string
122+
format: date-time
123+
required:
124+
- error
125+
- timestamp
126+
```
127+
128+
**Output:**
129+
130+
```python
131+
# generated by datamodel-codegen:
132+
# filename: api.yaml
133+
# timestamp: 2019-07-26T00:00:00+00:00
134+
135+
from __future__ import annotations
136+
137+
from datetime import datetime
138+
139+
from mypackage.shared.models import Error, User
140+
from pydantic import BaseModel
141+
142+
143+
class UserResponse(BaseModel):
144+
user: User
145+
request_id: str
146+
147+
148+
class ErrorResponse(BaseModel):
149+
error: Error
150+
timestamp: datetime
151+
```
152+
153+
---
154+
77155
## `--input` {#input}
78156

79157
Specify the input schema file path.

docs/cli-reference/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This documentation is auto-generated from test cases.
88

99
| Category | Options | Description |
1010
|----------|---------|-------------|
11-
| 📁 [Base Options](base-options.md) | 9 | Input/output configuration |
11+
| 📁 [Base Options](base-options.md) | 10 | Input/output configuration |
1212
| 🔧 [Typing Customization](typing-customization.md) | 29 | Type annotation and import behavior |
1313
| 🏷️ [Field Customization](field-customization.md) | 24 | Field naming and docstring behavior |
1414
| 🏗️ [Model Customization](model-customization.md) | 39 | Model generation behavior |
@@ -77,6 +77,7 @@ This documentation is auto-generated from test cases.
7777
- [`--encoding`](base-options.md#encoding)
7878
- [`--enum-field-as-literal`](typing-customization.md#enum-field-as-literal)
7979
- [`--enum-field-as-literal-map`](typing-customization.md#enum-field-as-literal-map)
80+
- [`--external-ref-mapping`](base-options.md#external-ref-mapping)
8081
- [`--extra-fields`](field-customization.md#extra-fields)
8182
- [`--extra-template-data`](template-customization.md#extra-template-data)
8283

docs/cli-reference/quick-reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ datamodel-codegen [OPTIONS]
1717
| Option | Description |
1818
|--------|-------------|
1919
| [`--encoding`](base-options.md#encoding) | Specify character encoding for input and output files. |
20+
| [`--external-ref-mapping`](base-options.md#external-ref-mapping) | Map external `$ref` files to Python packages. |
2021
| [`--input`](base-options.md#input) | Specify the input schema file path. |
2122
| [`--input-file-type`](base-options.md#input-file-type) | Specify the input file type for code generation. |
2223
| [`--input-model`](base-options.md#input-model) | Import a Python type or dict schema from a module. |
@@ -252,6 +253,7 @@ All options sorted alphabetically:
252253
- [`--encoding`](base-options.md#encoding) - Specify character encoding for input and output files.
253254
- [`--enum-field-as-literal`](typing-customization.md#enum-field-as-literal) - Convert all enum fields to Literal types instead of Enum cla...
254255
- [`--enum-field-as-literal-map`](typing-customization.md#enum-field-as-literal-map) - Override enum/literal generation per-field via JSON mapping.
256+
- [`--external-ref-mapping`](base-options.md#external-ref-mapping) - Map external `$ref` files to Python packages.
255257
- [`--extra-fields`](field-customization.md#extra-fields) - Configure how generated models handle extra fields not defin...
256258
- [`--extra-template-data`](template-customization.md#extra-template-data) - Pass custom template variables from JSON file for code gener...
257259
- [`--field-constraints`](field-customization.md#field-constraints) - Generate Field() with validation constraints from schema.

src/datamodel_code_generator/prompt_data.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"--encoding": "Specify character encoding for input and output files.",
4747
"--enum-field-as-literal": "Convert all enum fields to Literal types instead of Enum classes.",
4848
"--enum-field-as-literal-map": "Override enum/literal generation per-field via JSON mapping.",
49+
"--external-ref-mapping": "Map external `$ref` files to Python packages.",
4950
"--extra-fields": "Configure how generated models handle extra fields not defined in schema.",
5051
"--extra-template-data": "Pass custom template variables from JSON file for code generation.",
5152
"--field-constraints": "Generate Field() with validation constraints from schema.",

0 commit comments

Comments
 (0)