TestBoxLab / chalice-spec

Chalice x APISpec x Pydantic plug-ins
MIT License
17 stars 7 forks source link

OpenAPI v3 Specification errors #18

Open luis199230 opened 1 year ago

luis199230 commented 1 year ago

I installed the following libraries

chalice==1.28.0 apispec==6.3.0 chalice_spec==0.6.0 pydantic==2.0.3

My initialization code is this:

spec = APISpec(
    title='test-with-openapi-v3',
    version='1.0.0',
    openapi_version="3.0.2",
    plugins=[
        PydanticPlugin()
    ]
)

spec.components.schema("CodeSchema", model=CodeSchema)
spec.components.schema("CodesResponse", model=CodesResponse)

my CodeSchema:

class CodeSchema(BaseModel):
    id: str
    status: str
    person: str = 'uuid'

my CodesResponse:

class CodesResponse(BaseModel):
    status: bool
    message: str
    data: List[CodeSchema]

When I validate in https://validator.swagger.io/validator/debug my url I find this error:

{
  "messages": [
    "attribute components.schemas.CodesResponse.$defs is unexpected"
  ],
  "schemaValidationMessages": [
    {
      "level": "error",
      "domain": "validation",
      "keyword": "oneOf",
      "message": "instance failed to match exactly one schema (matched 0 out of 2)",
      "schema": {
        "loadingURI": "#",
        "pointer": "/definitions/Components/properties/schemas/patternProperties/^[a-zA-Z0-9\\.\\-_]+$"
      },
      "instance": {
        "pointer": "/components/schemas/CodesResponse"
      }
    }
  ]
}

I was researching and I realized in OpenAPI v3 not support that reference and I would like to know if possible to solve from your side or apisec side?

jakemwood commented 1 year ago

There may have been a breaking change in Pydantic that has caused this issue. I'll see if I can investigate at some point! Thanks!

Ali-Toosi commented 2 months ago

I have this issue as well and can't figure out what exactly needs to change. The problem happens when you use nested Pydantic models. The nested model gets defined inside the parent model and Swagger isn't happy with that:

class NestedModel(BaseModel):
    name: str

class ParentModel(BaseModel):
    message: str
    data: NestedModel

Generates this when used as the response model in Operation:

"components": {
    "schemas": {
      "ParentModel": {
        "$defs": {                  <----- Swagger doesn't seem to accept
          "NestedModel": {
            "properties": {
              "name": {
                "title": "Name",
                "type": "string"
              },
            },
            "required": [
              "name",
            ],
            "title": "NestedModel",
            "type": "object"
          }
        },
        "properties": {
          "message": {
            "title": "Message",
            "type": "string"
          },
          "data": {
            "$ref": "#/components/schemas/NestedModel",         <----- Invalid
            "default": null
          }
        },
        "required": [
          "message"
        ],
        "title": "ParentModel",
        "type": "object"
      },
}

Did you come up with any workarounds by any chance @luis199230 ?

Ali-Toosi commented 2 months ago

Here's a fix: https://github.com/TestBoxLab/chalice-spec/pull/22