Skip to content

Parameter's allowEmptyValue and allowReserved should only be used for query parameters #72

@evroon

Description

@evroon

In the following example, I have one path parameter

from openapi_pydantic import (
    DataType,
    Info,
    OpenAPI,
    Operation,
    Parameter,
    ParameterLocation,
    PathItem,
    Response,
    Schema,
    Tag,
)

open_api = OpenAPI(
    info=Info(
        title="My own API",
        version="v0.0.1",
        description="Some API",
    ),
    openapi="3.1.1",
    paths={
        "/customers/{customer_id}": PathItem(
            get=Operation(
                operationId="123",
                tags=["customers"],
                parameters=[
                    Parameter(
                        name="customer_id",
                        param_in=ParameterLocation.PATH,
                        required=True,
                        schema=Schema(type=DataType.INTEGER, format="int64"),
                    )
                ],
                responses={"200": Response(description="pong")},
            )
        )
    },
    tags=[Tag(name="customers", description="Customer related operations")],
)

print(open_api.model_dump_json(by_alias=True, exclude_none=True, indent=2))

When I pass the output to vacuum, I get an error: (via vacuum lint -beds openapi_test.json)

openapi_test.json:20:11 | error | schema invalid: multiple components failed validation | oas3-schema | Schemas | $.paths['/customers/{customer_id}'].get.parameters[0]                                                   

17 |         ],
18 |         "operationId": "123",
19 |         "parameters": [
20 |           {
21 |             "required": true,

It complains about the schema because it wants allowEmptyValue and allowReserved not to be there. If I remove allowEmptyValue and allowReserved, it passes validation. I think it's because those two fields should only be used for query parameters (as the docstrings in openapi-pydantic also mention here and here).

So is there a way to define path parameters without allowEmptyValue and allowReserved? Adding exclude_unset to model_dump_json does work properly as a workaround in this case. But maybe that should be documented here as well then, like you do for exclude_none=True?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions