-
-
Notifications
You must be signed in to change notification settings - Fork 437
Add schema_features property to parsers for version detection #2929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
811cf13
68ffafa
e31aa17
d382f26
eef2db7
21f338f
de56f28
2cbbda6
b0c5101
c81aaa8
943a8f8
17741e1
c83f9c1
ed273ba
50d8a6c
8078f37
e93346c
da95f2d
35bd40f
332cccd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,8 @@ | |
| | [`--input-model`](#input-model) | Import a Python type or dict schema from a module. | | ||
| | [`--input-model-ref-strategy`](#input-model-ref-strategy) | Strategy for referenced types when using --input-model. | | ||
| | [`--output`](#output) | Specify the destination path for generated Python code. | | ||
| | [`--schema-version`](#schema-version) | Schema version to use for parsing. | | ||
| | [`--schema-version-mode`](#schema-version-mode) | Schema version validation mode. | | ||
| | [`--url`](#url) | Fetch schema from URL with custom HTTP headers. | | ||
|
|
||
| --- | ||
|
|
@@ -326,6 +328,290 @@ is written to stdout. | |
|
|
||
| --- | ||
|
|
||
| ## `--schema-version` {#schema-version} | ||
|
|
||
| Schema version to use for parsing. | ||
|
|
||
| The `--schema-version` option specifies the schema version to use instead of auto-detection. | ||
| Valid values depend on input type: JsonSchema (draft-04, draft-06, draft-07, 2019-09, 2020-12) | ||
| or OpenAPI (3.0, 3.1). Default is 'auto' (detected from $schema or openapi field). | ||
|
|
||
| !!! tip "Usage" | ||
|
|
||
| ```bash | ||
| datamodel-codegen --input schema.json --schema-version draft-07 # (1)! | ||
| ``` | ||
|
|
||
| 1. :material-arrow-left: `--schema-version` - the option documented here | ||
|
|
||
| ??? example "Examples" | ||
|
|
||
| === "OpenAPI" | ||
|
|
||
| **Input Schema:** | ||
|
|
||
| ```yaml | ||
| openapi: "3.0.0" | ||
| info: | ||
| version: 1.0.0 | ||
| title: Swagger Petstore | ||
| license: | ||
| name: MIT | ||
| servers: | ||
| - url: http://petstore.swagger.io/v1 | ||
| paths: | ||
| /pets: | ||
| get: | ||
| summary: List all pets | ||
| operationId: listPets | ||
| tags: | ||
| - pets | ||
| parameters: | ||
| - name: limit | ||
| in: query | ||
| description: How many items to return at one time (max 100) | ||
| required: false | ||
| schema: | ||
| type: integer | ||
| format: int32 | ||
| responses: | ||
| '200': | ||
| description: A paged array of pets | ||
| headers: | ||
| x-next: | ||
| description: A link to the next page of responses | ||
| schema: | ||
| type: string | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: "#/components/schemas/Pets" | ||
| default: | ||
| description: unexpected error | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: "#/components/schemas/Error" | ||
| x-amazon-apigateway-integration: | ||
| uri: | ||
| Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations | ||
| passthroughBehavior: when_no_templates | ||
| httpMethod: POST | ||
| type: aws_proxy | ||
| post: | ||
| summary: Create a pet | ||
| operationId: createPets | ||
| tags: | ||
| - pets | ||
| responses: | ||
| '201': | ||
| description: Null response | ||
| default: | ||
| description: unexpected error | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: "#/components/schemas/Error" | ||
| x-amazon-apigateway-integration: | ||
| uri: | ||
| Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations | ||
| passthroughBehavior: when_no_templates | ||
| httpMethod: POST | ||
| type: aws_proxy | ||
| /pets/{petId}: | ||
| get: | ||
| summary: Info for a specific pet | ||
| operationId: showPetById | ||
| tags: | ||
| - pets | ||
| parameters: | ||
| - name: petId | ||
| in: path | ||
| required: true | ||
| description: The id of the pet to retrieve | ||
| schema: | ||
| type: string | ||
| responses: | ||
| '200': | ||
| description: Expected response to a valid request | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: "#/components/schemas/Pets" | ||
| default: | ||
| description: unexpected error | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: "#/components/schemas/Error" | ||
| x-amazon-apigateway-integration: | ||
| uri: | ||
| Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations | ||
| passthroughBehavior: when_no_templates | ||
| httpMethod: POST | ||
| type: aws_proxy | ||
| components: | ||
| schemas: | ||
| Pet: | ||
| required: | ||
| - id | ||
| - name | ||
| properties: | ||
| id: | ||
| type: integer | ||
| format: int64 | ||
| default: 1 | ||
| name: | ||
| type: string | ||
| tag: | ||
| type: string | ||
| Pets: | ||
| type: array | ||
| items: | ||
| $ref: "#/components/schemas/Pet" | ||
| Users: | ||
| type: array | ||
| items: | ||
| required: | ||
| - id | ||
| - name | ||
| properties: | ||
| id: | ||
| type: integer | ||
| format: int64 | ||
| name: | ||
| type: string | ||
| tag: | ||
| type: string | ||
| Id: | ||
| type: string | ||
| Rules: | ||
| type: array | ||
| items: | ||
| type: string | ||
| Error: | ||
| description: error result | ||
| required: | ||
| - code | ||
| - message | ||
| properties: | ||
| code: | ||
| type: integer | ||
| format: int32 | ||
| message: | ||
| type: string | ||
| apis: | ||
| type: array | ||
| items: | ||
| type: object | ||
| properties: | ||
| apiKey: | ||
| type: string | ||
| description: To be used as a dataset parameter value | ||
| apiVersionNumber: | ||
| type: string | ||
| description: To be used as a version parameter value | ||
| apiUrl: | ||
| type: string | ||
| format: uri | ||
| description: "The URL describing the dataset's fields" | ||
| apiDocumentationUrl: | ||
| type: string | ||
| format: uri | ||
| description: A URL to the API console for each API | ||
| Event: | ||
| type: object | ||
| description: Event object | ||
| properties: | ||
| name: | ||
| type: string | ||
| Result: | ||
| type: object | ||
| properties: | ||
| event: | ||
| $ref: '#/components/schemas/Event' | ||
| ``` | ||
|
|
||
| **Output:** | ||
|
|
||
| > **Error:** File not found: openapi/api.py | ||
|
|
||
| === "JSON Schema" | ||
|
|
||
| **Input Schema:** | ||
|
|
||
| ```json | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema", | ||
| "type": "object", | ||
| "properties": {"s": {"type": ["string"]}}, | ||
| "required": ["s"] | ||
| } | ||
| ``` | ||
|
|
||
| **Output:** | ||
|
|
||
| ```python | ||
| # generated by datamodel-codegen: | ||
| # filename: simple_string.json | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class Model(BaseModel): | ||
| s: str | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## `--schema-version-mode` {#schema-version-mode} | ||
|
|
||
| Schema version validation mode. | ||
|
|
||
| The `--schema-version-mode` option controls how schema version validation is performed. | ||
| 'lenient' (default): accept all features regardless of version. | ||
| 'strict': warn on features outside the declared/detected version. | ||
|
|
||
| !!! tip "Usage" | ||
|
|
||
| ```bash | ||
| datamodel-codegen --input schema.json --schema-version-mode lenient # (1)! | ||
| ``` | ||
|
|
||
| 1. :material-arrow-left: `--schema-version-mode` - the option documented here | ||
|
|
||
| ??? example "Examples" | ||
|
|
||
| **Input Schema:** | ||
|
|
||
| ```json | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema", | ||
| "type": "object", | ||
| "properties": {"s": {"type": ["string"]}}, | ||
| "required": ["s"] | ||
| } | ||
| ``` | ||
|
|
||
| **Output:** | ||
|
|
||
| ```python | ||
| # generated by datamodel-codegen: | ||
| # filename: simple_string.json | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class Model(BaseModel): | ||
| s: str | ||
| ``` | ||
|
|
||
| --- | ||
|
Comment on lines
+569
to
+613
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add examples demonstrating lenient vs. strict mode behavior. The current example doesn't illustrate the difference between Consider adding examples that:
This will help users understand when and why to use each mode. 🤖 Prompt for AI Agents |
||
|
|
||
| ## `--url` {#url} | ||
|
|
||
| Fetch schema from URL with custom HTTP headers. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve example output and consider simplifying the OpenAPI example.
Issues with the documentation examples:
Error message as output (line 537): The OpenAPI example shows
> **Error:** File not found: openapi/api.pyas the output, which appears to be a placeholder or incorrect content. This will confuse users.Verbose OpenAPI example (lines 354-533): The 180-line OpenAPI specification is excessive for demonstrating the
--schema-versionoption. A simpler example would be more effective.Examples don't demonstrate the option's effect: Neither example clearly shows what happens when you specify
--schema-version draft-07versus auto-detection or a different version. Consider adding examples that highlight version-specific behavior (e.g., using features from different JSON Schema drafts).📋 Suggested improvements
For the OpenAPI example, either:
For demonstrating the option's purpose, consider adding an example that shows:
exclusiveMinimumas boolean in Draft 4 vs. number in Draft 6+)--schema-versionaffects the generated output or validation behavior🤖 Prompt for AI Agents