Skip to content

strict_nullable should be True #473

@akkuman

Description

@akkuman

openapi.json

{
  "openapi": "3.0.1",
  "info": {
    "title": "test",
    "description": "",
    "version": "1.0.0"
  },
  "tags": [],
  "paths": {
    "/test": {
      "post": {
        "summary": "test",
        "deprecated": false,
        "description": "",
        "tags": [],
        "parameters": [],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "nullable_field": {
                    "type": "string",
                    "nullable": true
                  }
                },
                "required": [
                  "nullable_field"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "status"
                  ]
                }
              }
            },
            "headers": {}
          }
        },
        "security": []
      }
    }
  },
  "components": {
    "schemas": {},
    "securitySchemes": {}
  },
  "servers": [],
  "security": {}
}

command

uvx --from fastapi-code-generator fastapi-codegen --input ./specs/openapi.json --output . --output-model-type pydantic_v2.BaseModel

models.py

result

# generated by fastapi-codegen:
#   filename:  ./specs/xiaozhi-server.openapitest.json
#   timestamp: 2025-02-11T06:09:59+00:00

from __future__ import annotations

from pydantic import BaseModel


class TestPostRequest(BaseModel):
    nullable_field: str


class TestPostResponse(BaseModel):
    status: str

expected

# generated by fastapi-codegen:
#   filename:  ./specs/xiaozhi-server.openapitest.json
#   timestamp: 2025-02-11T06:07:27+00:00

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel, Field


class TestPostRequest(BaseModel):
    nullable_field: Optional[str] = Field(...)


class TestPostResponse(BaseModel):
    status: str

ref

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions