#OpenAPI Breaking with Validators

1 messages · Page 1 of 1 (latest)

potent void
#

Hey everyone,

I have a graph-like object, and a pydantic validator that checks that the source & target of the edges are node ids that are also a part of the payload. This essentially validates that every edge is connected to a node that exists. I've attached the pydantic definitions and the validators.
Now if I try to generate an OpenAPI schema where I have any API that takes GraphDto as a payload, it fails with the following error:

{
status_code: 400,
detail: null,
value_error: "1 validation error for GraphDto
edges
  Invalid edges: wMvXnxifKuSqOCVwKKmq,wMvXnxifKuSqOCVwKKmq don't have a targets or the target nodes don't exist (type=value_error)"
}

It tries to generate random values based on the default function provided where it generates a shortuuid if the value is not a part of the payload. Is there a way to avoid this, and force a particular payload so the OpenAPI schema can be generated properly, or perhaps disable the validators just for the OpenAPI schema generation? I tried playing around with OpenAPIConfig config parameters such as random_seed or generate_examples to no avail.

Frankly, I'm not confident that I'm approaching this correct and would love to have some guidance. Thank you!

fallow mangoBOT
#
Notes for OpenAPI Breaking with Validators
At your assistance

@potent void

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.

quiet bough
#

when you say "it fails". Can you elaborate a bit? Do the swagger docs render at all?

potent void
#

Hey again, the docs don't render at all it just returns a validation error JSON. I debugged this a little more, and it looks like the error message was misleading. The actual issue, is that one of the fields of one of my DTOs is defined as the following:
display_name: str = Field(default_factory=lambda: shortuuid.uuid(), max_length=600)
We use pydantic v1.
Now, it appears that the max_length attribute is what's causing the issue. I remember I created an issue where the API methods failed to parse such DTOs, and this was fixed in 2.8 IIRC, but looks like the same case is broken with openapi schema generations. It doesn't render the doc, it just returns the following error:

  for base in value.__class__.__mro__[:-1]:
        try:
            encoder = type_encoders[base]
            return encoder(value)
        except KeyError:
            continue
    raise TypeError(f"Unsupported type: {type(value)!r}")
def default_deserializer(
    target_type: Any, value: Any, type_decoders: TypeDecodersSequence | None = None
) -> Any:  # pragma: no cover
    """Transform values non-natively supported by ``msgspec``
quiet bough
#

ok, the last one appeared to be related to the examples getting generated by the swagger docs

#

out of curiosity, do the litestar schema export CLI commands fail?

potent void
#

Hey, sorry, missed your message from earlier. Same error with CLI

potent void
#

Hey @quiet bough, should I create a bug for this in github? It seems like a basic case where openapi generation fails with the above error when we use pydantic(test with v1) for request body validation and the max_len field.

quiet bough
#

Please. That would be helpful

#

Thanks

potent void
#

Will do, thanks!

potent void