Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAPI document generator does not account for required keyword when determining if a property is required #4878

Open
thorhj opened this issue May 2, 2024 · 2 comments

Comments

@thorhj
Copy link

thorhj commented May 2, 2024

Generated Open API definition does not mark properties as required if I use the required keyword on the properties.

Consider this request body type:

public record PatchUserRequest
{
  public string? Message { get; init; }
  public required int Age { get; init; }
}

When generating the Open API definition using NSwag, this type schema is generated:

      "PatchUserRequest": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "message": {
            "type": "string",
            "nullable": true
          },
          "age": {
            "type": "integer",
            "format": "int32"
          }
        }
      }

Notice there are no required properties in this object. I think the usage of required on the property should add the property to the required list in the object schema. Right now I have to annotate the property with [Required] to get the desired effect.

With or without RequiredAttribute, deserialization to this object fails if "age" is omitted in the JSON if I use the default JSON serializer for ASP.NET (System.Text.Json). I think Open API document generation should match this behavior and mark these properties as required.

I think this can be solved by looking for the RequiredMemberAttribute in addition to RequiredAttribute when determining if a property is required or not. RequiredMemberAttribute is generated by the compiler for required properties.

@Alex69rus
Copy link

Alex69rus commented May 8, 2024

I'm struggling with the same issue.
@thorhj have you found any workaround (except RequiredAttribute) ?

@thorhj
Copy link
Author

thorhj commented May 22, 2024

@Alex69rus Sorry, no.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants