Così Penso di avere un problema simile. Sto cercando swagger per generare enum insieme con int -> mapping delle stringhe. L'API deve accettare l'int. Lo swagger-ui conta di meno, quello che voglio veramente è la generazione del codice con un enum "vero" sull'altro lato (le app Android che usano il retrofit in questo caso).
Quindi dalla mia ricerca questo alla fine sembra essere un limite delle specifiche OpenAPI utilizzate da Swagger. Non è possibile specificare nomi e numeri per le enumerazioni.
Il problema migliore che ho trovato è https://github.com/OAI/OpenAPI-Specification/issues/681 che sembra un "forse presto", ma Swagger dovrebbe essere aggiornato, e nel mio caso anche Swashbuckle.
Per ora la mia soluzione è stata quella di implementare un filtro di documento che cerca enumerazioni e popola la descrizione pertinente con il contenuto dell'enumerazione.
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.DocumentFilter<SwaggerAddEnumDescriptions>();
//disable this
//c.DescribeAllEnumsAsStrings()
SwaggerAddEnumDescriptions.cs:
using System;
using System.Web.Http.Description;
using Swashbuckle.Swagger;
using System.Collections.Generic;
public class SwaggerAddEnumDescriptions : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
// add enum descriptions to result models
foreach (KeyValuePair<string, Schema> schemaDictionaryItem in swaggerDoc.definitions)
{
Schema schema = schemaDictionaryItem.Value;
foreach (KeyValuePair<string, Schema> propertyDictionaryItem in schema.properties)
{
Schema property = propertyDictionaryItem.Value;
IList<object> propertyEnums = [email protected];
if (propertyEnums != null && propertyEnums.Count > 0)
{
property.description += DescribeEnum(propertyEnums);
}
}
}
// add enum descriptions to input parameters
if (swaggerDoc.paths.Count > 0)
{
foreach (PathItem pathItem in swaggerDoc.paths.Values)
{
DescribeEnumParameters(pathItem.parameters);
// head, patch, options, delete left out
List<Operation> possibleParameterisedOperations = new List<Operation> { pathItem.get, pathItem.post, pathItem.put };
possibleParameterisedOperations.FindAll(x => x != null).ForEach(x => DescribeEnumParameters(x.parameters));
}
}
}
private void DescribeEnumParameters(IList<Parameter> parameters)
{
if (parameters != null)
{
foreach (Parameter param in parameters)
{
IList<object> paramEnums = [email protected];
if (paramEnums != null && paramEnums.Count > 0)
{
param.description += DescribeEnum(paramEnums);
}
}
}
}
private string DescribeEnum(IList<object> enums)
{
List<string> enumDescriptions = new List<string>();
foreach (object enumOption in enums)
{
enumDescriptions.Add(string.Format("{0} = {1}", (int)enumOption, Enum.GetName(enumOption.GetType(), enumOption)));
}
return string.Join(", ", enumDescriptions.ToArray());
}
}
Questo traduce in qualcosa di simile al seguente sul spavalderia-ui così almeno si possono "vedere quello che stai facendo": 
fonte
2017-03-28 15:42:58
Do vuoi che lo schema descriva il valore come una stringa, ma poi pubblica un intero sul server? JSON.net gestirà entrambi i valori, quindi la versione solo intera è un requisito definito? Non penso che Swagger supporti un tipo di enum con valore sia di stringa sia di intero. – Mig
Il tuo comportamento previsto non è chiaro, puoi spiegare meglio cosa vuoi visualizzare l'interfaccia utente di Swagger e cosa vuoi POST/PUT con la tua API Web con esempi? –
Mig è esattamente quello che volevo. Quando viene inviato al mio server non riuscendo a convalidare il numero intero, forse il problema è all'interno del mio codice. Ma tu dici che nel mio database, verrà salvato come intero? O sarà salvato come intero? Sarà uno spreco salvarlo come stringa sul mio database –