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

OpenAPI v3 Specification errors #18

Open
luis199230 opened this issue Jul 24, 2023 · 3 comments
Open

OpenAPI v3 Specification errors #18

luis199230 opened this issue Jul 24, 2023 · 3 comments

Comments

@luis199230
Copy link

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
Copy link
Contributor

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
Copy link

Ali-Toosi commented Sep 26, 2024

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
Copy link

Here's a fix: #22

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

No branches or pull requests

3 participants