zymlabs / nswag-fluentvalidation

Use FluentValidation rules instead of ComponentModel attributes to define swagger schema.
MIT License
59 stars 13 forks source link

No longer add NotNull/NotEmpty complex type properties as "oneOf" #7

Closed aretsof closed 4 years ago

aretsof commented 4 years ago

NSwag.FluentValidation

When a class has a complex type as a property and a validator sets it as NotNull() or NotEmpty(), currently a schema property such as below is created:

"complexType": {
    "minLength": 1,
    "nullable": false,
    "oneOf": [{
            "$ref": "#/components/schemas/ComplexType"
        }
    ]
}

Due to the fact that "oneOf" is used, the "Example Value" shown by Swagger UI doesn't include the property.

With the changes in this PR, the generated swagger.json schema property now looks like this:

"complexType": {
    "minLength": 1,
    "nullable": false,
    "$ref": "#/components/schemas/ComplexType"
}

Which no longer uses "oneOf", and therefore results in a correct "Example Value" in Swagger UI and more closely represents the swagger.json that is generated when the [Required] attribute is used.

geoffreytran commented 4 years ago

Great catch, I've been wondering why that wasn't working! I'll get it merged in later today and released.

geoffreytran commented 4 years ago

I've deployed v0.4.0 with these changes. I also noticed that the issue occurs also with c# enums if you want to take a stab at it. Redoc seems to display both correctly, but Swagger UI has issues with the complex type enums in the example display.