📘 OpenAPI-only Options¶
📋 Options¶
| Option | Description |
|---|---|
--include-path-parameters |
Include OpenAPI path parameters in generated parameter model... |
--openapi-scopes |
Specify OpenAPI scopes to generate (schemas, paths, paramete... |
--read-only-write-only-model-type |
Generate separate request and response models for readOnly/w... |
--use-operation-id-as-name |
Use OpenAPI operationId as the generated function/class name... |
--validation |
Enable validation constraints (deprecated, use --field-const... |
--include-path-parameters¶
Include OpenAPI path parameters in generated parameter models.
The --include-path-parameters flag adds path parameters (like /users/{userId})
to the generated request parameter models. By default, only query parameters
are included. Use this with --openapi-scopes parameters to generate parameter
models that include both path and query parameters.
Usage
datamodel-codegen --input schema.json --include-path-parameters --openapi-scopes schemas paths parameters # (1)!
-
--include-path-parameters- the option documented here
Input Schema
openapi: "3.0.0"
info:
version: 1.0.0
title: API with Path Parameters
paths:
/users/{userId}/posts/{postId}:
get:
summary: Get a specific post by user
operationId: getUserPost
parameters:
- name: userId
in: path
required: true
schema:
type: integer
- name: postId
in: path
required: true
schema:
type: string
- name: includeComments
in: query
required: false
schema:
type: boolean
responses:
'200':
description: A post
content:
application/json:
schema:
$ref: "#/components/schemas/Post"
components:
schemas:
Post:
type: object
properties:
id:
type: string
title:
type: string
content:
type: string
Output
# generated by datamodel-codegen:
# filename: include_path_parameters.yaml
# timestamp: 2019-07-26T00:00:00+00:00
from __future__ import annotations
from typing import Optional
from pydantic import BaseModel
class Post(BaseModel):
id: Optional[str] = None
title: Optional[str] = None
content: Optional[str] = None
class UsersUserIdPostsPostIdGetParameters(BaseModel):
userId: int
postId: str
includeComments: Optional[bool] = None
--openapi-scopes¶
Specify OpenAPI scopes to generate (schemas, paths, parameters).
The --openapi-scopes flag configures the code generation behavior.
Usage
-
--openapi-scopes- the option documented here
Input Schema
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
description: |
This description is for testing
multi-line
description
servers:
- url: http://petstore.swagger.io/v1
security:
- BearerAuth: []
paths:
/pets:
$ref: '#/components/pathItems/Pets'
/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/Pet"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
put:
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
summary: update a pet
tags:
- pets
requestBody:
required: false
content:
application/json:
schema:
$ref: '#/components/schemas/PetForm'
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
/food:
post:
summary: Create a food
tags:
- pets
requestBody:
required: true
content:
application/problem+json:
schema:
type: string
responses:
'201':
description: Null response
default:
description: unexpected error
content:
application/problem+json:
schema:
type: string
/food/{food_id}:
get:
summary: Info for a specific pet
operationId: showFoodById
tags:
- foods
parameters:
- name: food_id
in: path
description: The id of the food to retrieve
schema:
type: string
- name: message_texts
in: query
required: false
explode: true
schema:
type: array
items:
type: string
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
type: integer
examples:
example-1:
value:
- 0
- 1
- 3
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
/foo:
get:
tags:
- foo
responses:
'200':
description: OK
content:
application/json:
schema:
type: string
parameters:
- $ref: '#/components/parameters/MyParam'
/bar:
post:
summary: Create a bar
tags:
- bar
requestBody:
content:
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/PetForm'
/user:
get:
tags:
- user
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
timestamp:
type: string
format: date-time
name:
type: string
age:
type: string
required:
- name
- timestamp
post:
tags:
- user
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
timestamp:
type: string
format: date-time
name:
type: string
age:
type: string
required:
- name
- timestamp
responses:
'201':
description: OK
/users:
get:
tags:
- user
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
type: object
properties:
timestamp:
type: string
format: date-time
name:
type: string
age:
type: string
required:
- name
- timestamp
post:
tags:
- user
requestBody:
required: true
content:
application/json:
schema:
type: array
items:
type: object
properties:
timestamp:
type: string
format: date-time
name:
type: string
age:
type: string
required:
- name
- timestamp
responses:
'201':
description: OK
components:
parameters:
MyParam:
name: foo
in: query
schema:
type: string
securitySchemes:
BearerAuth:
type: http
scheme: bearer
schemas:
Pet:
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
Error:
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
PetForm:
title: PetForm
type: object
properties:
name:
type: string
age:
type: integer
pathItems:
Pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
security: []
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
default: 0
type: integer
format: int32
- name: HomeAddress
in: query
required: false
schema:
default: 'Unknown'
type: string
- name: kind
in: query
required: false
schema:
default: dog
type: string
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:
type: array
items:
- $ref: "#/components/schemas/Pet"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
post:
summary: Create a pet
tags:
- pets
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PetForm'
responses:
'201':
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
Output
# generated by datamodel-codegen:
# filename: body_and_parameters.yaml
# timestamp: 2019-07-26T00:00:00+00:00
from __future__ import annotations
from datetime import datetime
from typing import List, Optional
from pydantic import BaseModel
class Pet(BaseModel):
id: int
name: str
tag: Optional[str] = None
class Error(BaseModel):
code: int
message: str
class PetForm(BaseModel):
name: Optional[str] = None
age: Optional[int] = None
class PetsGetResponse(BaseModel):
__root__: List[Pet]
class FoodFoodIdGetResponse(BaseModel):
__root__: List[int]
class UserGetResponse(BaseModel):
timestamp: datetime
name: str
age: Optional[str] = None
class UserPostRequest(BaseModel):
timestamp: datetime
name: str
age: Optional[str] = None
class UsersGetResponseItem(BaseModel):
timestamp: datetime
name: str
age: Optional[str] = None
class UsersGetResponse(BaseModel):
__root__: List[UsersGetResponseItem]
class UsersPostRequestItem(BaseModel):
timestamp: datetime
name: str
age: Optional[str] = None
class UsersPostRequest(BaseModel):
__root__: List[UsersPostRequestItem]
--read-only-write-only-model-type¶
Generate separate request and response models for readOnly/writeOnly fields.
The --read-only-write-only-model-type option controls how models with readOnly or writeOnly
properties are generated. The 'request-response' mode creates separate Request and Response
variants for each schema that contains readOnly or writeOnly fields, allowing proper type
validation for API requests and responses without a shared base model.
Usage
datamodel-codegen --input schema.json --output-model-type pydantic_v2.BaseModel --read-only-write-only-model-type request-response # (1)!
-
--read-only-write-only-model-type- the option documented here
Input Schema
openapi: "3.0.0"
info:
title: Read Only Write Only Test API
version: "1.0"
paths: {}
components:
schemas:
User:
type: object
required:
- id
- name
- password
properties:
id:
type: integer
readOnly: true
name:
type: string
password:
type: string
writeOnly: true
created_at:
type: string
format: date-time
readOnly: true
secret_token:
type: string
writeOnly: true
Output
# generated by datamodel-codegen:
# filename: read_only_write_only.yaml
# timestamp: 2019-07-26T00:00:00+00:00
from __future__ import annotations
from typing import Optional
from pydantic import AwareDatetime, BaseModel
class UserRequest(BaseModel):
name: str
password: str
secret_token: Optional[str] = None
class UserResponse(BaseModel):
id: int
name: str
created_at: Optional[AwareDatetime] = None
--use-operation-id-as-name¶
Use OpenAPI operationId as the generated function/class name.
The --use-operation-id-as-name flag configures the code generation behavior.
Usage
datamodel-codegen --input schema.json --use-operation-id-as-name --openapi-scopes paths schemas parameters # (1)!
-
--use-operation-id-as-name- the option documented here
Input Schema
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
# generated by datamodel-codegen:
# filename: api.yaml
# timestamp: 2019-07-26T00:00:00+00:00
from __future__ import annotations
from typing import List, Optional
from pydantic import AnyUrl, BaseModel, Field
class Pet(BaseModel):
id: int
name: str
tag: Optional[str] = None
class Pets(BaseModel):
__root__: List[Pet]
class User(BaseModel):
id: int
name: str
tag: Optional[str] = None
class Users(BaseModel):
__root__: List[User]
class Id(BaseModel):
__root__: str
class Rules(BaseModel):
__root__: List[str]
class Error(BaseModel):
code: int
message: str
class Api(BaseModel):
apiKey: Optional[str] = Field(
None, description='To be used as a dataset parameter value'
)
apiVersionNumber: Optional[str] = Field(
None, description='To be used as a version parameter value'
)
apiUrl: Optional[AnyUrl] = Field(
None, description="The URL describing the dataset's fields"
)
apiDocumentationUrl: Optional[AnyUrl] = Field(
None, description='A URL to the API console for each API'
)
class Apis(BaseModel):
__root__: List[Api]
class Event(BaseModel):
name: Optional[str] = None
class Result(BaseModel):
event: Optional[Event] = None
class ListPetsParametersQuery(BaseModel):
limit: Optional[int] = None
--validation¶
Enable validation constraints (deprecated, use --field-constraints).
The --validation flag configures the code generation behavior.
Deprecated: Use --field-constraints instead
Usage
-
--validation- the option documented here
Input Schema
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
# generated by datamodel-codegen:
# filename: api.yaml
# timestamp: 2019-07-26T00:00:00+00:00
from __future__ import annotations
from typing import List, Optional
from pydantic import AnyUrl, BaseModel, Field
class Pet(BaseModel):
id: int
name: str
tag: Optional[str] = None
class Pets(BaseModel):
__root__: List[Pet]
class User(BaseModel):
id: int
name: str
tag: Optional[str] = None
class Users(BaseModel):
__root__: List[User]
class Id(BaseModel):
__root__: str
class Rules(BaseModel):
__root__: List[str]
class Error(BaseModel):
code: int
message: str
class Api(BaseModel):
apiKey: Optional[str] = Field(
None, description='To be used as a dataset parameter value'
)
apiVersionNumber: Optional[str] = Field(
None, description='To be used as a version parameter value'
)
apiUrl: Optional[AnyUrl] = Field(
None, description="The URL describing the dataset's fields"
)
apiDocumentationUrl: Optional[AnyUrl] = Field(
None, description='A URL to the API console for each API'
)
class Apis(BaseModel):
__root__: List[Api]
class Event(BaseModel):
name: Optional[str] = None
class Result(BaseModel):
event: Optional[Event] = None