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

bug: SecuritySchemeData root_validator conflicts with alias #350

Open
jelmerk opened this issue Oct 3, 2023 · 2 comments
Open

bug: SecuritySchemeData root_validator conflicts with alias #350

jelmerk opened this issue Oct 3, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@jelmerk
Copy link

jelmerk commented Oct 3, 2023

Describe the bug

Impossible to create SecuritySchemeData with field_in field because of the alias="in" field on the pydantic field_in field

To Reproduce

from spectree.models import InType, SecureType, SecuritySchemeData

if __name__ == "__main__":
    foo = SecuritySchemeData(
        type=SecureType.API_KEY,
        name="X-MIRO-ID",
        field_in=InType.HEADER,
        description="Miro passport authentication",
        scheme="passport",
    )

Expected behavior

Should create a valid SecuritySchemeData but instead raises this exception

pydantic.error_wrappers.ValidationError: 1 validation error for SecuritySchemeData
__root__
  For `apiKey` type `name, field_in` field(s) is required. (type=value_error)

The spectree version

Name: spectree
Version: 1.2.3
Summary: generate OpenAPI document and validate request&response with Python annotations.
Home-page:
Author:
Author-email: Keming Yang kemingy94@gmail.com
License: Apache-2.0
Location: /Users/jkuperus/Library/Caches/pypoetry/virtualenvs/data-workflow-service-VxjL5469-py3.8/lib/python3.8/site-packages
Requires: pydantic
Required-by:
Darwin Jelmers-MacBook-Pro.local 22.6.0 Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000 arm64
Python 3.8.10

Additional context

As a workaround you can do

SecuritySchemeData(
        **{
             "type": SecureType.API_KEY,
             "name": "X-MIRO-ID",
             "in": InType.HEADER,
             "scheme": "passport",
             "description": "Miro passport authentication",
         }
     )
@kemingy
Copy link
Member

kemingy commented Oct 3, 2023

Hi @jelmerk, thanks for the feedback.

This is a bit tricky. Alias here is necessary since “in” is a keyword in Python. Let me check if we can make it work with the “root_validator”.

@kemingy kemingy changed the title bug: <title> bug: SecuritySchemeData root_validator conflicts with alias Oct 5, 2023
@kemingy
Copy link
Member

kemingy commented Oct 7, 2023

I found that field with alias need to pass the aliased name.

class Model(BaseModel):
    schema_x: str = Field(
        description="xxx",
        alias="schema"
    )
Model(schema_x="xyz")

This will panic since schema is required. But in is a keyword, so the only way to use it is passing it in a dict ("in" instead of in).

I didn't find any other solutions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants