Skip to content
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

Elegantly handle Pydantic model schema #168

Merged
merged 4 commits into from
Dec 26, 2022
Merged

Elegantly handle Pydantic model schema #168

merged 4 commits into from
Dec 26, 2022

Conversation

ahopkins
Copy link
Member

Closes #152

class Alert(BaseModel):
    hit: dict[str, Any]
    last_updated: datetime


@pydataclass
class AlertResponse:
    alert: Alert
    rule_id: str

This will automatically drop all references and pydantic classes into components:

{
  "openapi": "3.0.3",
  "info": {
    "title": "API",
    "version": "1.0.0",
    "contact": {}
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "get~handler",
        "summary": "Handler",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/AlertResponse"
              }
            }
          }
        },
        "responses": {
          "default": {
            "description": "OK"
          }
        }
      }
    }
  },
  "tags": [],
  "servers": [],
  "security": [],
  "components": {
    "schemas": {
      "Alert": {
        "title": "Alert",
        "type": "object",
        "properties": {
          "hit": {
            "title": "Hit",
            "type": "object"
          },
          "last_updated": {
            "title": "Last Updated",
            "type": "string",
            "format": "date-time"
          }
        },
        "required": [
          "hit",
          "last_updated"
        ]
      },
      "AlertResponse": {
        "title": "AlertResponse",
        "type": "object",
        "properties": {
          "alert": {
            "$ref": "#/components/schemas/Alert"
          },
          "rule_id": {
            "title": "Rule Id",
            "type": "string"
          }
        },
        "required": [
          "alert",
          "rule_id"
        ]
      }
    }
  }
}

@ahopkins ahopkins marked this pull request as draft December 26, 2022 14:22
@ahopkins ahopkins marked this pull request as ready for review December 26, 2022 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] When Pydantic model refers to another model, swagger errors out and redoc crashes with an error
1 participant