[PHP-Symfony] revamp the computation of the contentType #21292
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master
(upcoming7.x.0
minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)Why this commit
recent commit 4089438 already improved that method: before that other commit it was just impossible to query a endpoint with a response type that was something else than
application/json
orapplication/xml
. With that commit it became possible to query such endpoint provided that the client declare in itsAccept
header that it can cope with*/*
(or provided that the client omitted that header altogether).But there were still cases badly handled. For instance if an endpoint returns a response of type
image/png
and that it receives a query with headerAccept: image/png
, then it would reply with406
(instead of just serving the image).To avoid any other issues with type resolution, this commit revamps the
getOutputFormat
function more thoroughly and does it by implementing the specification (available athttps://httpwg.org/specs/rfc9110.html#field.accept ), which means in particular that the format accepted by the client are ordered by the relative weights specified (if any).
Nb: note that merging this PR would make it possible to close the PR #15560 since this one is a kind of superset of that other one.
How to test this commit
Use a simple openapi specification with an endpoint that can serve several different response type. I used:
then generate the symfony bundle and create a Symfony app with a placeholder implementation of the generated interface. I used:
then I ran against that server the following curl queries, with the
-i
flag to check the content type of the response (nb: we don't care at all about the body of the response: only the content type header is important for those tests):Some basic checks:
curl -i -H 'Accept: text/plain' http://localhost:8000/pet/1
=> response should have content typetext/plain
(without this patch this query yields a406
)curl -i -H 'Accept: text/html' http://localhost:8000/pet/1
=> response should have content typetext/html
(it receives a406
without this patchcurl -i -H 'Accept: application/json' http://localhost:8000/pet/1
=> should beapplication/json
when there are not common format
curl -i -H 'Accept: image/png' http://localhost:8000/pet/1
=> should return a406
queries with weight
curl -i -H 'Accept: text/html, application/xml ; q=0.8' http://localhost:8000/pet/1
=> when there are no explicit weight then the default value is 1. So response should have content typetext/html
(nb: this query also has a bunch of whitespaces in theAccept
header to make sure it is correctly handled). (nb: without this commit, the response has content typeapplication/xml
, which does not conform to the spec)curl -i -H 'Accept: text/html;q=0.7, application/xml ; q=0.8' http://localhost:8000/pet/1
=> this ensure it works when the format with higher priority is not provided first in the listcurl -i -H 'Accept: image/png;q=0.9,text/html;q=0.8' http://localhost:8000/pet/1
=> tests that when the format with higher value is not supported, we correctly fallback to another onecurl -i -H 'Accept: text/*, application/xml ; q=0.8' http://localhost:8000/pet/1
=> tests that we can match when the subtype is a wildcardcurl -i -H 'Accept: */*' http://localhost:8000/pet/1
=> tests that we can match when both the type and subtype are wildcardTesting with parameters in the header
curl -i -H 'Accept: text/plain;format=fixed;q=0.4' http://localhost:8000/pet/1
=> response should have content typetext/plain
curl -i -H 'Accept: text/plain;format=fixed' http://localhost:8000/pet/1
=> same test but without explicit weightcurl -i -H 'Accept: text/plain;format=fixed;q=0.4,text/html;q=0.5' http://localhost:8000/pet/1
=> response should betext/html
curl -i -H 'Accept: text/plain;format=fixed;q=0.4,text/html;q=0.3' http://localhost:8000/pet/1
> response should betext/plain
curl -i -H 'Accept: text/plain;format=fixed,text/html;q=0.3' http://localhost:8000/pet/1
=> response should betext/plain
Test with some badly formatted query, to ensure we can fallback to an acceptable format instead of crashing
curl -i -H 'Accept: text;q=0.9, application/xml ; q=0.8' http://localhost:8000/pet/1
=> the type with higher weigh has no subtype, which is invalid. With this commit we receive a response of typeapplication/xml
in thise case (as is the case without this commit)curl -i -H 'Accept: application/xml ; q=0.8,' http://localhost:8000/pet/1
=> the header has a trailing comma. This ensure we can still reply a response of typeapplication/xml
Edit: I forgot to ping the technical committee: @jebentier @dkarlovi @mandrean @jfastnacht @ybelenko @renepardon