I've recently started using ASP.NET Boilerplate as a starting point for a project I'm working on - Asp.NET Core Web Api + Angular, and I'm having trouble finding some information regarding something that seems to me like a common use issue. I'll try to explain what I mean the best I can.
What I want to do is to have an API endpoint that takes a parameter that needs to be restricted to a specific set of values - so, I wanted to use Enum for this purpose.
This all works nice as far as parameter validation goes, but I'm confused about how to make this work well with swagger and shared proxy generated services for fontend by Nswag.
Enum parameter is displayed as integer in the documentation parameter example, so it's not clear just by looking at the swagger documentation what is the set of available values, because int values have no meaning to the user that is looking at the documentation.
Lets use an example like this, user input used in one of app service methods:
Service method:
public async Task DoSomething(SomethingInput input)
{
//some code handling user input
}
Input DTO:
public class SomethingInput : EntityDto<string>
{
[EnumDataType(typeof(SomethingEnum))]
public SomethingEnum Something { get; set; }
public int SomeOtherData { get; set; }
}
Enum:
public enum SomethingEnum
{
Option1,
Option2,
Option3
}
In this case, available values for SomethingEnum in SomethingInput will be shown as 0, 1, 3, instead of "Option1", "Option2", "Option2".
I tried using DescribeAllEnumsAsStrings for swagger generation options which helps with the documentation, but it creates an issue in the Angular project functionality - to be specific, changing tenant fails because of the way enums are generated using DescribeAllEnumsAsStrings by Nswag tool - it expects values to be integers, rather than strings.
export enum IsTenantAvailableOutputState {
Available = <any>"Available",
InActive = <any>"InActive",
NotFound = <any>"NotFound",
}
Response value returned by API is numeric, and this code doesn't know how to parse the value correctly anymore because enum no longer has integer values:
export class AppTenantAvailabilityState {
static Available: number = IsTenantAvailableOutputState._1;
static InActive: number = IsTenantAvailableOutputState._2;
static NotFound: number = IsTenantAvailableOutputState._3;
}
The question here is, what is the best practice for having these kind of restrained parameters in routes or DTO objects used as user inputs, so that they are correctly displayed in swagger doc, and do not break down anything in angular project when regenerating shared proxies with Nswag tool?