microsoft / OpenAPI.NET

The OpenAPI.NET SDK contains a useful object model for OpenAPI documents in .NET along with common serializers to extract raw OpenAPI JSON and YAML documents from the model.
MIT License
1.4k stars 230 forks source link

Required attribute of non-required query parameters does not get rendered #427

Open jbroenstrup opened 4 years ago

jbroenstrup commented 4 years ago

I am a bit confused concerning the openapi serialization.

This code

var document = new OpenApiDocument
{
    Components = new OpenApiComponents()
};
document.Components.Parameters.Add("foo",
    new OpenApiParameter()
    {
        Required = true,
        Name = "bar",
        In = ParameterLocation.Query
    });

using (var sw = new StringWriter())
{
    var writer = new OpenApiJsonWriter(sw);
    document.SerializeAsV3(writer);
    var openapiDefinition = sw.ToString();
    Console.WriteLine(openapiDefinition);
}

creates this openapi definition:

{
  "openapi": "3.0.1",
  "info": { },
  "paths": { },
  "components": {
    "parameters": {
      "foo": {
        "name": "bar",
        "in": "query",
        "required": true
      }
    }
  }
}

But if I set the parameter to be not required, the required property is missing from the openapi definition of the parameter entirely.

This is a problem, because the third party tool I am using to generate client code for this API depends on said required property to always being there, either true or false.

Is there a possibility to force the serialization to include the required property?

darrelmiller commented 4 years ago

Hmm, that's unfortunate. The required property is only required when the in property is set to path. http://spec.openapis.org/oas/v3.0.2#fixed-fields-9

What is the tool that you are using? Maybe we can convince them to fix their tool.

davidkvc commented 3 years ago

I have encountered the same behavior even when in property is set to query. I believe that in that case the required property should ALWAYS be rendered. Microsoft.OpenApi version 1.2.3