Ask Your Question
4

What is the process for incorporating OneOf declaration in OpenAPI 3 using Swashbuckle?

asked 2022-05-11 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-03-08 13:00:00 +0000

huitzilopochtli gravatar image

To incorporate OneOf declaration in OpenAPI 3 using Swashbuckle, follow the steps below:

  1. Define your model classes with the OneOf attribute. For example:
public class Person
{
    public string Name { get; set; }
}

[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(Student), "student")]
public class Student : Person
{
    public string School { get; set; }
}

[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(Employee), "employee")]
public class Employee : Person
{
    public string Company { get; set; }
}
  1. In your Swagger configuration, add a schema filter that uses the OneOfSchemaFilter class. For example:
services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
    c.SchemaFilter<OneOfSchemaFilter>();
});
  1. Define the OneOfSchemaFilter class. This class will iterate through all the model classes and generate the appropriate OpenAPI schema. For example:
public class OneOfSchemaFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        var type = context.Type;

        if (type == typeof(Student) || type == typeof(Employee))
        {
            schema.OneOf = new List<OpenApiSchema>
            {
                context.SchemaGenerator.GenerateSchema(typeof(Student), context.SchemaRepository),
                context.SchemaGenerator.GenerateSchema(typeof(Employee), context.SchemaRepository)
            };
        }
    }
}
  1. Use the OneOf attribute in your action method to specify the response type. For example:
[HttpGet("{id}")]
[ProducesResponseType(typeof(Student), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Employee), StatusCodes.Status200OK)]
public IActionResult GetPerson(int id)
{
    if (id % 2 == 0)
    {
        return Ok(new Student { Name = "John", School = "ABC University" });
    }
    else
    {
        return Ok(new Employee { Name = "Jane", Company = "XYZ Inc." });
    }
}
  1. Run your application and navigate to Swagger UI. You should see the OneOf schema displayed for your response type.

Note: The Swashbuckle.AspNetCore.Newtonsoft NuGet package is required for the JsonSubtypes attribute to work with Swashbuckle.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-05-11 11:00:00 +0000

Seen: 7 times

Last updated: Mar 08 '22