Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger bitwise enum flags handling

I have an enum like;

[Flags]
public enum MeteoType : ushort
{
    Wind = 1 << 0,
    Pressure = 1 << 1,
    Temperature = 1 << 2,
    Waves = 1 << 4,
    Currents = 1 << 9,
    Swell = 1 << 13,
    WindWave = 1 << 14,
}

and I have a model;

public class Params
{
    public List<MeteoType> Meteo { get; set; } = new List<MeteoType>()
    {
        MeteoType.Wind
    };
    ....
}

In my controller method, I ask for this model from query as such;

public async Task<IActionResult> Get(int z, int x, int y, [FromQuery] Params parameters)

Which gives me this view in swagger:

enter image description here

I'm using List because it's a flag and I want to be able to choose multiple elements. The problem starts here. When I choose multiple elements the link is created like:

https://localhost:44311?Meteo=1&Meteo=2&Meteo=4&...

rather than

https://localhost:44311?Meteo=7&...

How can I make it so that link is generated by sum of enum values, rather than all of them one by one?

like image 528
OhWelp Avatar asked May 28 '26 21:05

OhWelp


1 Answers

OpenAPI Specification does not support bitwise enum parameters. Your Meteo parameter needs to be defined as just type: integer in the OpenAPI definition, i.e. you need to tweak the annotations so that they produce type: integer instead of type: array for this parameter. The consumers will need to provide the correct summed value manually.

# OpenAPI definition generated from the code

openapi: 3.0.0
...

paths:
  /something:
    get:

      parameters:
        - in: query
          name: Meteo
          schema:
            type: integer
          example: 7
          description: >-
            One of the following values, or a sum of multiple values:

             * 1 - Wind
             * 2 - Pressure
             * ...

            For example, 7 means Wind (1) + Pressure (2) + Temperature (4).

Alternatively, you can try forking Swagger UI and change the code to send the selected list values as a single summed value instead of multi-value parameter.

like image 161
Helen Avatar answered May 30 '26 11:05

Helen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!