RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.78k stars 1.29k forks source link

Objects with oneOf members not appearing in SwaggerUI #2748

Open justinc13 opened 4 years ago

justinc13 commented 4 years ago

Hello,

Using NSwag.AspNetCore 13.3.0, given an API controller that takes in the following model:

public class Person
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public Car Car { get; set; }
}

public class Car
{
    public string Make { get; set; }

    public string Model { get; set; }

    public int Year { get; set; }
}

the following OpenAPI schema is generated:

{
  "Person": {
    "type": "object",
    "additionalProperties": false,
    "properties": {
      "firstName": {
        "type": "string",
        "nullable": true
      },
      "lastName": {
        "type": "string",
        "nullable": true
      },
      "car": {
         "nullable": true,
         "oneOf": [
          {
            "$ref": "#/components/schemas/Car"
          }
        ]
      }
    }
  },
  "Car": {
    "type": "object",
    "additionalProperties": false,
    "properties": {
      "make": {
        "type": "string",
        "nullable": true
      },
      "model": {
        "type": "string",
        "nullable": true
      },
      "year": {
        "type": "integer",
        "format": "int32"
      }
    }
  }
}

Unfortunately, the SwaggerUI doesn't correctly render the 'Car' property in the example: image

It exists within the schema definition in the SwaggerUI though: image

Is this correct behaviour? Is there maybe a configuration property for this?

markoilic-dev commented 4 years ago

I have had the same issue. What throws me off as well is that it shows it as "oneOf", but really there is no other option?

https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/

justinc13 commented 4 years ago

Would I be able to get an update on this issue?

blackgate commented 4 years ago

I am also seeing this with classes that implement List<T>. Changing the type directly to List<T> or IEnumerable<T> solves the problem.

RicoSuter commented 4 years ago

Set AllowReferencesWithProperties to false See https://github.com/RicoSuter/NSwag/issues/2986#issuecomment-700624325