@@ -245,7 +245,7 @@ This documentation is auto-generated from test cases.
245245
246246| Category | Options | Description |
247247|----------|---------|-------------|
248- | 📁 [Base Options](base-options.md) | 9 | Input/output configuration |
248+ | 📁 [Base Options](base-options.md) | 10 | Input/output configuration |
249249| 🔧 [Typing Customization](typing-customization.md) | 29 | Type annotation and import behavior |
250250| 🏷️ [Field Customization](field-customization.md) | 24 | Field naming and docstring behavior |
251251| 🏗️ [Model Customization](model-customization.md) | 39 | Model generation behavior |
@@ -314,6 +314,7 @@ This documentation is auto-generated from test cases.
314314- [`--encoding`](base-options.md#encoding)
315315- [`--enum-field-as-literal`](typing-customization.md#enum-field-as-literal)
316316- [`--enum-field-as-literal-map`](typing-customization.md#enum-field-as-literal-map)
317+ - [`--external-ref-mapping`](base-options.md#external-ref-mapping)
317318- [`--extra-fields`](field-customization.md#extra-fields)
318319- [`--extra-template-data`](template-customization.md#extra-template-data)
319320
@@ -480,6 +481,7 @@ Source: https://datamodel-code-generator.koxudaxi.dev/cli-reference/base-options
480481| Option | Description |
481482|--------|-------------|
482483| [`--encoding`](#encoding) | Specify character encoding for input and output files. |
484+ | [`--external-ref-mapping`](#external-ref-mapping) | Map external `$ref` files to Python packages. |
483485| [`--input`](#input) | Specify the input schema file path. |
484486| [`--input-file-type`](#input-file-type) | Specify the input file type for code generation. |
485487| [`--input-model`](#input-model) | Import a Python type or dict schema from a module. |
@@ -549,6 +551,83 @@ Default is UTF-8, which is the standard encoding for JSON and most modern text f
549551
550552---
551553
554+ ## `--external-ref-mapping` {#external-ref-mapping}
555+
556+ Map external `$ref` files to Python packages.
557+
558+ Use `--external-ref-mapping FILE_PATH=PYTHON_PACKAGE` to import referenced models from an existing package,
559+ instead of generating duplicate classes from external schema files.
560+
561+ !!! tip "Usage"
562+
563+ ```bash
564+ datamodel-codegen --input schema.json --input-file-type openapi --external-ref-mapping common.yaml=mypackage.shared.models # (1)!
565+ ```
566+
567+ 1. :material-arrow-left: `--external-ref-mapping` - the option documented here
568+
569+ ??? example "Examples"
570+
571+ **Input Schema:**
572+
573+ ```yaml
574+ openapi: "3.0.3"
575+ info:
576+ title: API
577+ version: "1.0.0"
578+ paths: {}
579+ components:
580+ schemas:
581+ UserResponse:
582+ type: object
583+ properties:
584+ user:
585+ $ref: "common.yaml#/components/schemas/User"
586+ request_id:
587+ type: string
588+ required:
589+ - user
590+ - request_id
591+ ErrorResponse:
592+ type: object
593+ properties:
594+ error:
595+ $ref: "common.yaml#/components/schemas/Error"
596+ timestamp:
597+ type: string
598+ format: date-time
599+ required:
600+ - error
601+ - timestamp
602+ ```
603+
604+ **Output:**
605+
606+ ```python
607+ # generated by datamodel-codegen:
608+ # filename: api.yaml
609+ # timestamp: 2019-07-26T00:00:00+00:00
610+
611+ from __future__ import annotations
612+
613+ from datetime import datetime
614+
615+ from mypackage.shared.models import Error, User
616+ from pydantic import BaseModel
617+
618+
619+ class UserResponse(BaseModel):
620+ user: User
621+ request_id: str
622+
623+
624+ class ErrorResponse(BaseModel):
625+ error: Error
626+ timestamp: datetime
627+ ```
628+
629+ ---
630+
552631## `--input` {#input}
553632
554633Specify the input schema file path.
@@ -23637,6 +23716,7 @@ datamodel-codegen [OPTIONS]
2363723716| Option | Description |
2363823717|--------|-------------|
2363923718| [`--encoding`](base-options.md#encoding) | Specify character encoding for input and output files. |
23719+ | [`--external-ref-mapping`](base-options.md#external-ref-mapping) | Map external `$ref` files to Python packages. |
2364023720| [`--input`](base-options.md#input) | Specify the input schema file path. |
2364123721| [`--input-file-type`](base-options.md#input-file-type) | Specify the input file type for code generation. |
2364223722| [`--input-model`](base-options.md#input-model) | Import a Python type or dict schema from a module. |
@@ -23872,6 +23952,7 @@ All options sorted alphabetically:
2387223952- [`--encoding`](base-options.md#encoding) - Specify character encoding for input and output files.
2387323953- [`--enum-field-as-literal`](typing-customization.md#enum-field-as-literal) - Convert all enum fields to Literal types instead of Enum cla...
2387423954- [`--enum-field-as-literal-map`](typing-customization.md#enum-field-as-literal-map) - Override enum/literal generation per-field via JSON mapping.
23955+ - [`--external-ref-mapping`](base-options.md#external-ref-mapping) - Map external `$ref` files to Python packages.
2387523956- [`--extra-fields`](field-customization.md#extra-fields) - Configure how generated models handle extra fields not defin...
2387623957- [`--extra-template-data`](template-customization.md#extra-template-data) - Pass custom template variables from JSON file for code gener...
2387723958- [`--field-constraints`](field-customization.md#field-constraints) - Generate Field() with validation constraints from schema.
0 commit comments