#Error accessing openapi.json when using SecretStr

1 messages · Page 1 of 1 (latest)

golden dune
#

Hey, all. I'm getting the following error when using a SecretStr attribute (from Pydantic) and accessing /schema/openapi.json: type object 'SecretStr' has no attribute 'item_type'. I've got SecretStr: str in my type_encoders, but that doesn't seem to help.

short shaleBOT
#
Notes for Error accessing openapi.json when using SecretStr
At your assistance

@golden dune

No Response?

If no response in a reasonable time, ping @Member.

Closing

To close, type !solve or byte solve.

MCVE

Please include an MCVE so that we can reproduce your issue locally.

lucid grove
# golden dune Hey, all. I'm getting the following error when using a `SecretStr` attribute (fr...

Heya, possible to share an MCVE? I tried with the following (on current main) it seems to work even without type_encoders={SecretStr: str}

from litestar import post
from pydantic import BaseModel, SecretStr

class Something(BaseModel):
    password: SecretStr

@post()
async def something(data: Something) -> Something:
    return data

from litestar.testing import create_test_client

with create_test_client([something]) as client:
    print(client.get('schema/openapi.json').json())
short shaleBOT
#
MCVE Needed to Reproduce!
Hi

@lucid grove

MCVE

Please include an MCVE so that we can reproduce your issue locally.

golden dune
#

Looks like it's an issue when using pydantic.Field. Your example works for me, but if you change it to the following, it breaks with that error:

from litestar import post
from pydantic import BaseModel, Field, SecretStr


class Something(BaseModel):
    password: SecretStr = Field(min_length=8)


@post()
async def something(data: Something) -> Something:
    return data


from litestar.testing import create_test_client

with create_test_client([something]) as client:
    print(client.get('schema/openapi.json').json())
lucid grove
#

@jovial oak seems like something you must know

jovial oak
#

Oh wow this is interesting.

jovial oak
#

Okay. Yeah this is a bug. For checking if a string is constrained we do an isinstance check, but unfortunately, SecretStr is not a subclass of str so we don't follow the logic of handling constrained strings, and so we end up with this issue. The same issue will be there for SecretBytes as well.

@golden dune if you'd like to create an issue for this on GitHub, that'd be great 🙂

#

Also, just to confirm, you're using pydantic v2 right?

golden dune
short shaleBOT
golden dune
#

That's pretty sick haha.

lucid grove
#

@stable raven you got a fan

stable raven
#

Needs more polish

#

want modal

rose roostBOT
stable raven
#

waiting for alc to pick it up

golden dune
#

Love that idea. I was going to say, it'd be nice if I could've customized the message to remove the "Hey, all" and add the MCVE. But didn't know you made this, Coffee.

lucid grove