core icon indicating copy to clipboard operation
core copied to clipboard

HeaderParameter and validation not coherent

Open Grummfy opened this issue 7 months ago • 2 comments

API Platform version(s) affected: 3.4 -> 4.1.x tested with my project (3.4) and validated that it still exist with master of api-platform/api-platform so I assume 4.x

Description
HeaderParameter constraint seems not coherent. Here, I tested the validation of an header parameter for the api. It seems to be inconsistent.

  • checking that it's required : worked great
  • checking that my value is less or equals than X : work great on the openapi front part, not on back part (no validation)
  • checking that my value is an integer : work great on the openapi front part, not on back part
  • checking that my value is greater or equals than X : failed

How to reproduce

I took the demo project, and on the greeting entity I have make this change

#[ApiResource(mercure: true)] to #[ApiResource(mercure: true, parameters: ['Foo' => new HeaderParameter(schema: ['type' => 'integer', 'example' => 3, 'minimum' => 1, 'maximum' => 5], required: true)])]

to check each test, maximum need to be removed to test the other constraint

Possible Solution
validation on the value of the array ?

Additional Context
Perhaps I missuses the HeaderParameter ? But the output in openApi format seems coherent

The constraint is applied on an array (don't know why an array instead of the value), so perhaps it's the reason of the behaviour?

thanks

Grummfy avatar Apr 23 '25 14:04 Grummfy

To clarify, the schema defines the JSON Schema definition of your Parameter, and this is used by OpenAPI (as its JSON-Schema compliant).

Based on these, in this part of the code, we're adding some automatic constraints:

https://github.com/api-platform/core/blob/04d5cff1b28b494ac2e90257a79ce6c045ba82ae/src/Validator/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactory.php#L92-L173

But we definitely do not cover the whole JSON Schema specification. The safest way to ensure that is to add your own constraint to the parameter. Feel free to contribute any new validation if you think it can be automatic!

soyuka avatar May 05 '25 12:05 soyuka

Thanks for the response. For me the problem is that it's incoherent and inconsistent.

So, the result is that I can't declare a proper schema because of this.

Grummfy avatar May 07 '25 11:05 Grummfy

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 06 '25 11:07 stale[bot]