#Help with OpenAPI schema generation

1 messages · Page 1 of 1 (latest)

static gate
#

Hey guys,

I have a graph object that has nodes and edges. Each edge has a source and a target which are references to node ids. I've implemented a custom field validator that checks that each source and target exist in the node list of the graph. It all works great, but the openapi schema generation fails because of this validator. Both the nodes and edges have a default factory for the id field in shortuuid.uuid(). The error is the error of the validator:

{
status_code: 400,
detail: null,
value_error: "1 validation error for GraphDto
edges
  Value error, Invalid edges: pZYhKCKNYmpNtrleyqIv,pZYhKCKNYmpNtrleyqIv don't have a targets, the target nodes don't exist, or the target equals the source [type=value_error, input_value=[Edge(id='pZYhKCKNYmp...kqgXYT', disabled=True)], input_type=list]
    For further information visit https://errors.pydantic.dev/2.9/v/value_error"
}

I'm trying to understand the right approach here. I was thinking to add an example on the pydantic field where I can specify hard-coded id values for 2 nodes and have one edge where the source and target is one of each. Using the examples field from here: https://docs.pydantic.dev/latest/concepts/json_schema/#customizing-json-schema doesn't seem to translate to the openapi schema generation.

With that, I wanted to have a discussion on what the right approach is in this case. I feel like providing pre-determined examples should work, but I can't understand from the documentation if it's even supported.

Thanks!

Litestar Version: 2.12.1
Pydantic Version: 2.9.2

half furnaceBOT
#
Notes for Help with OpenAPI schema generation
At your assistance

@static gate

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.

dense jacinth
#

<@&1084813023044173866>

grim wave
#

@static gate can you show your model? It should work if you've set the examples using the Field in pydantic, if I remember correctly.

static gate
#

Hey @grim wave , here is the part of the model that's relevant to my issue here:

class Node(BaseModel):
    id: str = Field(default_factory=lambda: shortuuid.uuid())
    type: NodeType
    data: dict[str, NodeParamData]
    position: NodePosition
    base_type: NodeBaseType
    disabled: bool = Field(default=False)

    @field_validator("type", mode="before")
    @classmethod
    def validate_enum_type(cls, flow_node_type_str: str):
        return NodeType(flow_node_type_str)

    @classmethod
    def generate_example(cls, id: str) -> "Node":
        data = dict()
        data["test_param"] = NodeParamData(value="test_value")
        return cls(id=id,
                   type=NodeType.SelectionChain,
                   data=data,
                   position=NodePosition(x=random.uniform(-1000, 1000), y=random.uniform(-1000, 1000)),
                   base_type=NodeBaseType.ABSTRACT)


class Edge(BaseModel):
    id: str = Field(default_factory=lambda: shortuuid.uuid())
    source: str
    target: str
    sourceHandle: str
    targetHandle: str
    disabled: bool = Field(default=False)

    @classmethod
    def generate_example(cls, source: str, target: str) -> "Edge":
        return cls(source=source,
                   target=target,
                   sourceHandle=source,
                   targetHandle=target,)


class NodesEdges(BaseModel):
    nodes: list[Node] = Field(examples=[[Node.generate_example("source_id"), FlowNode.generate_example("target_id")]])
    edges: list[Edge] = Field(examples=[[Edge.generate_example(source="source_id", target="target_id")]])
    
#

When the openapi schema is generated, the object has one node with a random short uuid, whereas I expect 2 with the ids "source_id" and "target_id"

static gate
#

@grim wave After digging a little bit more, it does actually use the examples in the generated schema if I disable the validator. It looks like that despite that, it goes through polyfactory to create random examples, so it fails the validation before it gets to use the examples I set. That's why I assumed it didn't work. Is there a way to bypass that polyfactory example generation? Obviously it's not required for fields that have examples that are later used in the schema. I did try setting create_examples as False(although it's False by default) in my openapi config.