Open santhoshhere opened 5 years ago
Use StringEnumConverter?
Isn't the second line of code does that? Or it should be written somewhere. Is there any code that would help me?
Can you post the generated spec?
the nswag.json? or the typescript enum ?
The openapi/swagger spec...
{
"runtime": "NetCore22",
"defaultVariables": null,
"swaggerGenerator": {
"webApiToSwagger": {
"controllerNames": [],
"isAspNetCore": true,
"resolveJsonOptions": false,
"defaultUrlTemplate": "api/{controller}/{id?}",
"addMissingPathParameters": false,
"includedVersions": null,
"defaultPropertyNameHandling": "Default",
"defaultReferenceTypeNullHandling": "Null",
"defaultEnumHandling": "Integer",
"flattenInheritanceHierarchy": false,
"generateKnownTypes": true,
"generateEnumMappingDescription": false,
"generateXmlObjects": false,
"generateAbstractProperties": false,
"ignoreObsoleteProperties": false,
"allowReferencesWithProperties": false,
"excludedTypeNames": [],
"serviceHost": null,
"serviceBasePath": null,
"serviceSchemes": [],
"infoTitle": "My Title",
"infoDescription": null,
"infoVersion": "1.0.0",
"documentTemplate": null,
"documentProcessorTypes": [],
"operationProcessorTypes": [],
"typeNameGeneratorType": null,
"schemaNameGeneratorType": null,
"contractResolverType": null,
"serializerSettingsType": null,
"useDocumentProvider": true,
"documentName": "v1",
"aspNetCoreEnvironment": null,
"createWebHostBuilderMethod": null,
"startupType": null,
"allowNullableBodyParameters": true,
"output": null,
"outputType": "Swagger2",
"assemblyPaths": [ "bin/Debug/netcoreapp2.2/WebAPI.dll" ],
"assemblyConfig": null,
"referencePaths": [],
"useNuGetCache": false
}
},
"codeGenerators": {
"swaggerToTypeScriptClient": {
"className": "{controller}Client",
"moduleName": "",
"namespace": "",
"typeScriptVersion": 2.7,
"template": "Angular",
"promiseType": "Promise",
"httpClass": "HttpClient",
"useSingletonProvider": false,
"injectionTokenType": "InjectionToken",
"rxJsVersion": 6.3,
"dateTimeType": "Date",
"nullValue": "Undefined",
"generateClientClasses": true,
"generateClientInterfaces": false,
"generateOptionalParameters": false,
"exportTypes": true,
"wrapDtoExceptions": false,
"clientBaseClass": null,
"wrapResponses": false,
"wrapResponseMethods": [],
"generateResponseClasses": true,
"responseClass": "SwaggerResponse",
"protectedMethods": [],
"configurationClass": null,
"useTransformOptionsMethod": false,
"useTransformResultMethod": false,
"generateDtoTypes": true,
"operationGenerationMode": "MultipleClientsFromOperationId",
"markOptionalProperties": true,
"generateCloneMethod": false,
"typeStyle": "Class",
"classTypes": [],
"extendedClasses": [],
"extensionCode": null,
"generateDefaultValues": true,
"excludedTypeNames": [],
"excludedParameterNames": [],
"handleReferences": false,
"generateConstructorInterface": true,
"convertConstructorInterfaceData": false,
"importRequiredTypes": true,
"useGetBaseUrlMethod": false,
"baseUrlTokenName": "API_BASE_URL",
"queryNullValue": "",
"inlineNamedDictionaries": false,
"templateDirectory": null,
"typeNameGeneratorType": null,
"propertyNameGeneratorType": null,
"enumNameGeneratorType": null,
"serviceHost": "https://localhost:44375",
"serviceSchemes": null,
"output": "../../Angular/src/app/services/services.ts"
}
}
}
This is the config, i need the generated spec..
When i execute the API with this config, i get the typescript code generated. Is that what you needed?
The first step generates the swagger.json - in the ui the first tab on the right
Ah sorry, descriptions and displayvalue are not preserved in c# clients because there is no field for that in swagger
What should i do now? How can i get the desired enum values in the Typescript from NSwag codegen? is there anyother way?
Let me quickly update the NJS wiki here:
https://github.com/RicoSuter/NJsonSchema/wiki/Enums https://github.com/RicoSuter/NJsonSchema/wiki/JsonSchemaGenerator#integer-vs-string-enumerations
The first step generates the swagger.json - in the ui the first tab on the right
Just FYI, i don't use any UI to generate the code. I execute the API with the help of config and it generates in the output folder mentioned in the config(nswag.json)
you can set the ouput here to also get the swagger:
{
"runtime": "NetCore22",
"defaultVariables": null,
"swaggerGenerator": {
"webApiToSwagger": {
...
"output": "swagger.json",
...
The generated spec is,
"BloodGroups": {
"type": "integer",
"description": "",
"x-enumNames": [
"OPositive",
"ONegative",
"APositive",
"ANegative",
"BPositive",
"BNegative",
"ABPositive",
"ABNegative"
],
"enum": [
0,
1,
2,
3,
4,
5,
6,
7
]
},
Let me quickly update the NJS wiki here:
https://github.com/RicoSuter/NJsonSchema/wiki/Enums https://github.com/RicoSuter/NJsonSchema/wiki/JsonSchemaGenerator#integer-vs-string-enumerations
I'm not completely understanding what is here, can you please put me in the right direction?
@santhoshhere Did you find a solution to this?
Follow this link, it has the solution, just 2 steps:
1- Add the converter you prefer: Or
2- Use the converter as enum attribute as below.
Or
Hi,
i'm also strugle with this - after update to 14.x and NET 8.
before that, my enums were generated properly - with the correct integer indexes next to it.
after update i get something like this - without StringEnumConverter:
export enum ChronoUserRightsEnum { _0 = 0, _1 = 1, _2 = 2, _3 = 3, _4 = 4, _5 = 5, }
and with EnumConverter something like this:
export enum ChronoUserRightsEnum { Firma = "Firma", Department = "Department", Person = "Person", Filiale = "Filiale", Group = "Group", Hierarchical = "Hierarchical", }
but what i really need would be like (with version 13.x):
export enum ChronoUserRightsEnum { Firma = 0, Department = 1, Person = 2, Filiale = 3, Group = 4, Hierarchical = 5, }
any hints or tips would be highly appreciated - thanks
@mbarmettler dir you find a solution? I have the same challenge here.
@mbarmettler Same issue 😢
Hi @alexAlchemy and @kavatari
i'v struggled a lot and gathered several posts and information to have this achieved.
i ended up using these additional config steps in my Startup.cs:
//adding
addional Swashbuckle / OpneApi configurations
c.SchemaFilter
NSwagEnumExtensionSchemaFilter:
` ///
if (context is null)
throw new ArgumentNullException(nameof(context));
if (context.Type.IsEnum)
schema.Extensions.Add("x-enumNames", new NSwagEnumOpenApiExtension(context));
}
}
public class NSwagEnumOpenApiExtension : IOpenApiExtension { private readonly SchemaFilterContext _context; public NSwagEnumOpenApiExtension(SchemaFilterContext context) { _context = context; }
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
string[] enums = Enum.GetNames(_context.Type);
JsonSerializerOptions options = new() { WriteIndented = true };
string value = JsonSerializer.Serialize(enums, options);
writer.WriteRaw(value);
}
} `
checkout this reference: https://github.com/RicoSuter/NSwag/issues/1234
FromQueryModelFilter:
`public class FromQueryModelFilter : IOperationFilter { public void Apply(OpenApiOperation operation, OperationFilterContext context) { var description = context.ApiDescription; if (description.HttpMethod.ToLower() != HttpMethod.Get.ToString().ToLower()) { // We only want to do this for GET requests, if this is not a // GET request, leave this operation as is, do not modify return; }
var actionParameters = description.ActionDescriptor.Parameters;
var apiParameters = description.ParameterDescriptions
.Where(p => p.Source.IsFromRequest)
.ToList();
if (actionParameters.Count == apiParameters.Count)
{
// If no complex query parameters detected, leave this operation as is, do not modify
return;
}
operation.Parameters = CreateParameters(actionParameters, operation.Parameters, context);
}
private IList<OpenApiParameter> CreateParameters(
IList<ParameterDescriptor> actionParameters,
IList<OpenApiParameter> operationParameters,
OperationFilterContext context)
{
var newParameters = actionParameters
.Select(p => CreateParameter(p, operationParameters, context))
.Where(p => p != null)
.ToList();
return newParameters.Any() ? newParameters : null;
}
private OpenApiParameter CreateParameter(
ParameterDescriptor actionParameter,
IList<OpenApiParameter> operationParameters,
OperationFilterContext context)
{
var operationParamNames = operationParameters.Select(p => p.Name);
if (operationParamNames.Contains(actionParameter.Name))
{
// If param is defined as the action method argument, just pass it through
return operationParameters.First(p => p.Name == actionParameter.Name);
}
if (actionParameter.BindingInfo == null)
{
return null;
}
var generatedSchema = context.SchemaGenerator.GenerateSchema(actionParameter.ParameterType, context.SchemaRepository);
var newParameter = new OpenApiParameter
{
Name = actionParameter.Name,
In = ParameterLocation.Query,
Schema = generatedSchema
};
return newParameter;
}
}`
hope this helps you to get the stuff generated as needed. cheers & happy coding
@mbarmettler Hey, many thanks for the response above.
I've been having quite a few issues beside the enum one so I'm going to try some alternatives and see if I have any better luck but if may come back to this solution 🙏
I used the nswag to generate the code in typescript from my EF core API. The enum based class doesn't work as expected when using [DisplayName] or [Description] annotation.
C#:
Generated Code:
As i'm binding the values to a dropdown(select) I need to use the (O +ve, O -ve).
I used the below code,
Any idea how to make this work? or am i wrong in anything?