How to define Enums as a Reusable Schema

Hi.
a number of endpoints I created include an enum parameter like this:

[ApiAllowableValues("serv", typeof(eServer))]
[ApiMember(IsRequired = true, AllowMultiple = false, Description = "The Server you will connect to.")]
public eServer serv { get; set; }

Thing is, when generating the openapi.json, eServer does not appear as a named schema in components/schemas. I do not want to put this eServer in a wrapper DTO.

Asking some online AI’s gives suggestions like adding:

Plugins.Add(new OpenApiFeature
{
    DefaultEnumType = "string", // Use "int" if you prefer numeric values
    UseInlineSchemaForEnums = false // Ensures enums are defined globally
});

but those two attributes don’t even exist in my version of ServiceStack (I’m using 6.11.0 atm), neither can I find any documentation that this would exist.

This is problematic for those that consume my services and use NSWAG to generate the object code, as the serv gets duplicated many times over.

Any insights on this? Thx.

The Open API schema generation behavior for enums isn’t customizable, your AI search is hallucinating as there was never any such support.

You can customize the returned JSON schema with the Operation Filters but you’d need to do the heavy lifting to rewrite the schema to have the output you want.

For API code generation you could use Add ServiceStack Reference support for cleaner code generation in multiple supported languages.

Alternatively if you upgrade to .NET 8 with Endpoint Routing you could use ASP .NET Core 8’s support for Open API v3 which NSwag should have better support for.

1 Like