Skip to content

Conversation

@felixhummel
Copy link

@felixhummel felixhummel commented Jan 15, 2026

We need clean specs to generate clean clients.

I propose to validate the spec in the tests.
I also made changes to make the spec compliant.
Please note my comment in cd496a5 concerning links.

Checklist:

  • Run pytest tests and no failed.
  • Run ruff check flask_openapi3 tests examples and no failed.
  • Run ruff format flask_openapi3 tests examples and no failed.
  • Run mypy flask_openapi3 and no failed.
  • Run mkdocs serve and no failed.

E           openapi_spec_validator.validation.exceptions.OpenAPIValidationError: {} should be non-empty
E
E           Failed validating 'minProperties' in schema['properties']['paths']['patternProperties']['^/']['else']['properties']['get']['properties']['responses']:
E               {'$comment': 'https://spec.openapis.org/oas/v3.1.0#responses-object',
E                'type': 'object',
E                'properties': {'default': {'$ref': '#/$defs/response-or-reference'}},
E                'patternProperties': {'^[1-5](?:[0-9]{2}|XX)$': {'$ref': '#/$defs/response-or-reference'}},
E                'minProperties': 1,
E                '$ref': '#/$defs/specification-extensions',
E                'unevaluatedProperties': False,
E                'if': {'$comment': 'either default, or at least one response code '
E                                   'property must exist',
E                       'patternProperties': {'^[1-5](?:[0-9]{2}|XX)$': False}},
E                'then': {'required': ['default']}}
E
E           On instance['paths']['/book']['get']['responses']:
E               {}
- Remove 'default' key if it's None to maintain OpenAPI spec compliance.
- Only return valid links.
- validate schemas in all tests

I am not entirely happy with the links, because we silently ignore
things that the user provided.
We should either

- raise exceptions and change the tests, so that only valid links can be
  specified (my preferred option) or
- warn the user. I did not implement this, because logging is not used
  at all and I wanted to keep the library's style intact.
@model_serializer(mode="wrap", when_used="json")
def _serialize(self, serializer, info):
data = serializer(self)
# Remove 'default' key if it's None to maintain OpenAPI spec compliance
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This question was created in #189, @ddorian do you remember why you did it at the time?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(IIRC) There are cases when "default=None" is needed to be documented. Like, the field requires a value, None is valid, and it's the default. I already use openapi-spec-validator and don't see what validation it breaks though from a quick check of the pull request.

@luolingchun
Copy link
Owner

@felixhummel Thanks for your contribution, validating against the OpenAPI specification is more secure.

I don't think links should be validated, this should be verified by the user themselves.

You should create a new test script instead of modifying all existing tests, which is not good for maintenance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants