I was working on a separate PR for an added feature and ran into the following error running tox on just the fresh repo-checkout, before any other changes. It appears to be a compatibility issue with Pydantic 2 having something to do with optionals vs required. It may be related to recent(ish) pydantic changes to how optionals vs defaults work (see also: https://github.com/pydantic/pydantic/issues/7161)
I am unfortunately not knowledgeable enough about schemas to know what the correct fix is here.
Pydantic 2.5.2
=============================================== FAILURES ===============================================
__________________________________ test_optional_and_computed_fields ___________________________________
@pytest.mark.skipif(not PYDANTIC_V2, reason="computed fields require Pydantic V2")
def test_optional_and_computed_fields() -> None:
api = construct_sample_api()
result = construct_open_api_with_schema_class(api)
assert result.components is not None
assert result.components.schemas is not None
req_schema = result.components.schemas["SampleRequest"]
assert isinstance(req_schema, Schema)
assert req_schema.properties is not None
assert req_schema.required is not None
resp_schema = result.components.schemas["SampleResponse"]
assert isinstance(resp_schema, Schema)
assert resp_schema.properties is not None
assert resp_schema.required is not None
# When validating:
# - required fields are still required
# - optional fields are still optional
# - computed fields don't exist
assert "req" in req_schema.properties
assert "opt" in req_schema.properties
assert "comp" not in req_schema.properties
assert set(req_schema.required) == {"req"}
# When serializing:
# - required fields are still required
# - optional fields become required
# - computed fields are required
assert "req" in resp_schema.properties
assert "opt" in resp_schema.properties
assert "comp" in resp_schema.properties
> assert set(resp_schema.required) == {"req", "comp", "opt"}
E AssertionError: assert {'comp', 'req'} == {'opt', 'comp', 'req'}
E Extra items in the right set:
E 'opt'
E Full diff:
E - {'opt', 'comp', 'req'}
E ? -------
E + {'comp', 'req'}
tests/util/test_optional_and_computed.py:53: AssertionError
__________________________________ test_optional_and_computed_fields ___________________________________
@pytest.mark.skipif(not PYDANTIC_V2, reason="computed fields require Pydantic V2")
def test_optional_and_computed_fields() -> None:
api = construct_sample_api()
result = construct_open_api_with_schema_class(api)
assert result.components is not None
assert result.components.schemas is not None
req_schema = result.components.schemas["SampleRequest"]
assert isinstance(req_schema, Schema)
assert req_schema.properties is not None
assert req_schema.required is not None
resp_schema = result.components.schemas["SampleResponse"]
assert isinstance(resp_schema, Schema)
assert resp_schema.properties is not None
assert resp_schema.required is not None
# When validating:
# - required fields are still required
# - optional fields are still optional
# - computed fields don't exist
assert "req" in req_schema.properties
assert "opt" in req_schema.properties
assert "comp" not in req_schema.properties
assert set(req_schema.required) == {"req"}
# When serializing:
# - required fields are still required
# - optional fields become required
# - computed fields are required
assert "req" in resp_schema.properties
assert "opt" in resp_schema.properties
assert "comp" in resp_schema.properties
> assert set(resp_schema.required) == {"req", "comp", "opt"}
E AssertionError: assert {'comp', 'req'} == {'opt', 'comp', 'req'}
E Extra items in the right set:
E 'opt'
E Full diff:
E - {'opt', 'comp', 'req'}
E ? -------
E + {'comp', 'req'}
tests/v3_0_3/test_optional_and_computed.py:56: AssertionError
======================================= short test summary info ========================================
FAILED tests/util/test_optional_and_computed.py::test_optional_and_computed_fields - AssertionError: assert {'comp', 'req'} == {'opt', 'comp', 'req'}
FAILED tests/v3_0_3/test_optional_and_computed.py::test_optional_and_computed_fields - AssertionError: assert {'comp', 'req'} == {'opt', 'comp', 'req'}
===================================== 2 failed, 44 passed in 1.08s =====================================
py311-pydantic2-test: exit 1 (2.05 seconds) /srv/personal/openapi-pydantic> poetry run pytest -vv tests pid=7099
py311-pydantic2-test: FAIL ✖ in 9.99 seconds
Hi @adam-homeboost - sorry for the slow reply - I haven't been able to reproduce this with the latest Pydantic after checking out - could you try updating and retry?
I was working on a separate PR for an added feature and ran into the following error running tox on just the fresh repo-checkout, before any other changes. It appears to be a compatibility issue with Pydantic 2 having something to do with optionals vs required. It may be related to recent(ish) pydantic changes to how optionals vs defaults work (see also: https://github.com/pydantic/pydantic/issues/7161)
I am unfortunately not knowledgeable enough about schemas to know what the correct fix is here.
Pydantic 2.5.2