Skip to content

OpenAPI: required element is missing for request bodies #1376

Closed
@bkoelman

Description

@bkoelman

A produced request body in OAS looks like:

        "requestBody": {
          "description": "The attributes and relationships of the node to create.",
          "content": {
            "application/vnd.api+json": {
              "schema": {
                "allOf": [
                  {
                    "$ref": "#/components/schemas/nodePostRequestDocument"
                  }
                ]
              }
            }
          }
        }

which means the request body is optional.

When generating a C# client using NSwag with:

<Options>/GenerateNullableReferenceTypes:true /GenerateOptionalPropertiesAsNullable:true</Options>

It generates the next method:

public virtual System.Threading.Tasks.Task<NodePrimaryResponseDocument> PostNodeAsync(NodePostRequestDocument? body)

This is incorrect because request bodies are never optional in JSON:API.

This can be fixed by generating the OAS file as below:

        "requestBody": {
          "description": "The attributes and relationships of the node to create.",
          "required": true, // <<<<<<<<<<<<<<
          "content": {
            ...
            }
          }
        }

which produces the correct method:

public virtual System.Threading.Tasks.Task<NodePrimaryResponseDocument> PostNodeAsync(NodePostRequestDocument body)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions