OpenAPI.NET icon indicating copy to clipboard operation
OpenAPI.NET copied to clipboard

Parameters Serialization - Style doesn't respect the default value

Open safepage opened this issue 3 years ago • 1 comments

This is similar to the issue in Pull Request 544.

There is now an inconsistency as Style is not set to a default value (but Explode is): “Default values (based on value of in): for query - form; for path - simple; for header - simple; for cookie - form.”

I assume the fix would be similar?

Although the serialization code for Explode also looks wrong:

writer.WriteProperty(OpenApiConstants.Explode, Explode, Style.HasValue && Style.Value == ParameterStyle.Form);

Maybe it should be:

writer.WriteProperty(OpenApiConstants.Explode, _explode);

Then the serialized model would reflect the actual values read in (as _explode is a nullable bool?).

safepage avatar May 12 '22 17:05 safepage

Thanks for reporting this. We will need to have a local field to track any explicitly set style value and if that is not set then return a style based on the In value. This does raise the question of why is In nullable if it is a required field. We can't put the In value in a constructor as that would make deserializing hard. We could set a default value in the class. I'm just not sure if that is a breaking change or not.

Can you tell me more about the scenario where you ran into this problem? Thanks.

darrelmiller avatar Jul 04 '22 20:07 darrelmiller

Sorry for the delay...

I had to write the following workaround, as the model can be saying parameter.Explode == false if it is not explicitly set in the OpenAPI document, but if you work it out from the rules then Explode may in fact need to be true:

`internal static bool GetExplodeValue(OpenApiParameter parameter) { bool explodeArray = true;

// can't trust parameter.Explode if we don't have a Style, so work it out from defaults

if (parameter.Style != null)
    explodeArray = parameter.Explode;
else if (parameter.In == ParameterLocation.Query || parameter.In == ParameterLocation.Cookie)
    explodeArray = true;
else
    explodeArray = false;

return explodeArray;

}`

So I think it may be better if Explode was set to null if it is not explicitly set in the OpenAPI as it is misleading, or actually work it out from the rules (but then the model doesn't reflect the document). The same applies to style.

safepage avatar Aug 25 '22 14:08 safepage

Let's not set a default for the In parameter because that would prevent us from knowing if a user has explicitly set it in the future. Let's create a backing property for style to know if the user has explicitly set it. If they have not set style then we should return a default value of style based on the In parameter. If the In parameter is null then the Style should return as Simple, as if the In was set to Path.

darrelmiller avatar Aug 30 '22 13:08 darrelmiller