cebe / php-openapi

Read and write OpenAPI yaml/json files and make the content accessible in PHP objects.
MIT License
466 stars 87 forks source link

Fix OpenAPI v3.0 Schema Violation: Array value found, but an object is required #125

Closed om4james closed 2 years ago

om4james commented 2 years ago

Hi there,

Thank you for your very useful tool, it's been really useful in our projects.

bin/php-openapi validate currently fails unexpectedly when validating a schema that specifies an empty value for a Map OpenAPI field.

For example, the minimal specification I've provided in c5ca5238d952d9f380507c1bd1401ce48bae1567 currently fails:

bin/php-openapi validate tests/spec/data/empty-maps.json
OpenAPI v3.0 schema violations:
- [paths./products.get.responses[200].headers] Array value found, but an object is required
- [paths./products.get.responses[200].content] Array value found, but an object is required
- [paths./products.get.responses[200].links] Array value found, but an object is required

Whereas that specification passes in various other validation tools such as https://apitools.dev/swagger-parser/online/ or https://www.jsonschemavalidator.net/.

This PR changes to the Serialization process so that empty Map Fields are correctly validated.

The example in this PR uses the Response object, however, the fix should apply for any Map type fields.

cebe commented 2 years ago

Rebased onto master here: https://github.com/cebe/php-openapi/pull/152

Thank you!