Open simeyla opened 6 years ago
Try check you have DataContractAttribute on TokenRequest in your C# code. Yesterday met with this bug)
I'm generating a Angular 6 Client with DTO Interfaces. The interface gets generated with uppercase property Name (first letter) instead of all lowercase. So I also end up with undefined because the JSON Parsing won't work. If i manually change the property names in the generated interface to lowercase (as it should be, also according to JS conventions), everything is working fine.
Generated using the NSwag toolchain v11.18.6.0 (NJsonSchema v9.10.67.0 (Newtonsoft.Json v9.0.0.0)) (http://NSwag.org)
Did you see my previous answer? Or you don't have code written in C#?
@sh2ezo I did see it - but that's not a good solution. I want to minimize unnecessary attributes.
There's definitely a bug in the generator. It modifies the casing in one place and not in another.
If you generate dto classes then the generated interface is for simple initializing the class via ctor and not to cast your JSON to it...
@RSuter
I agree, but I'm generating interfaces only ("typeStyle": "Interface"
).
The generated code parses the responseText with JSON.parse
:
result200 = _responseText === "" ? null : <MyInterface>JSON.parse(_responseText, this.jsonParseReviver);
This won't throw any error, but results in an object with undefined
properties (because the response text is lowercase, but the generated interface properties are not).
The interface properties are 1:1 to the defined properties in the json-schema (swagger-file). I think they should be transformed to lower-case.
I generate the swagger json from my AspNetCore MVC API, so for now I added the JsonProperty annotation to the C# Model:
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
Ah, I see... If you use the "old" generator (UseSwagger() and not UseSwaggerWithApiExplorer()) you have to change the DefaultPropertyNameHandling to CamelCase (setting) because this (the serializer is configured to serialize to lower camel case) is not automatically detected.
… if you look at the generated spec - are the property names correct according to the serialized JSON or are they already wrong in the spec?
The property names were already wrong (uppercase) in the generated spec. I'm already using the Api Explorer, but changing the DefaultPropertyNameHandling to CamelCase solved it, the spec is now generated with lower case property names, and now the generated Typescript interfaces are also lowercae and everything is working fine.
Thanks for your help!
Hmm strange, api explorer should auto detect this, can you post the c# code of the middleware registration + settings?
Do not set any of these settings here: https://github.com/RSuter/NSwag/blob/master/src/NSwag.AspNetCore/Middlewares/AspNetCoreToSwaggerMiddleware.cs#L83
Then the settings should be auto detected, can you confirm?
The generated spec in the API is and was perfectly fine!
app.UseSwaggerUi3WithApiExplorer(settings => {
settings.PostProcess = document => {
document.Info.Version = "v1.0.0";
document.Info.Title = "My API";
document.Info.Description = "My API Description";
document.Info.Contact = new NSwag.SwaggerContact {
Name = "My Compay",
Email = "mail@company.com",
Url = "https://www.company.com"
};
};
});
So this is auto detected like you said.
But if I generate the spec with NSwagStudio (I want to switch to MS Build later), I have to specify DefaultPropertyNameHandling to CamelCase:
{
"runtime": "NetCore21",
"defaultVariables": null,
"swaggerGenerator": {
"aspNetCoreToSwagger": {
"project": "../myapi/myapi.csproj",
"msBuildProjectExtensionsPath": null,
"configuration": "Debug",
"runtime": "",
"targetFramework": null,
"noBuild": true,
"verbose": false,
"workingDirectory": null,
"defaultPropertyNameHandling": "CamelCase",
"defaultReferenceTypeNullHandling": "Null",
"defaultEnumHandling": "Integer",
"flattenInheritanceHierarchy": false,
"generateKnownTypes": true,
"generateXmlObjects": false,
"generateAbstractProperties": false,
"ignoreObsoleteProperties": false,
"allowReferencesWithProperties": false,
"excludedTypeNames": [],
"serviceHost": null,
"serviceBasePath": null,
"serviceSchemes": [],
"infoTitle": "My API",
"infoDescription": null,
"infoVersion": "1.0.0",
"includedVersions": null,
"documentTemplate": null,
"documentProcessorTypes": [],
"operationProcessorTypes": [],
"typeNameGeneratorType": null,
"schemaNameGeneratorType": null,
"contractResolverType": null,
"serializerSettingsType": null,
"output": "myApiSpec.json",
"outputType": "OpenApi3",
"assemblyPaths": [],
"assemblyConfig": null,
"referencePaths": []
}
},
"codeGenerators": {}
}
Ah, I think for that to work we need the same logic here
and here
I think this PR fixes that + makes using the same settings in the middleware and CLI easier: https://github.com/RSuter/NSwag/pull/1430
Not sure yet if the actual implementation has the best possible design
For this definition in JSON I have
Username
andPassword
properties.The angular client I'm generating contains the following interface for an
ITokenRequest
and (as expected) makes the properties lowercase to conform to JS conventions.However the
init()
function does not change the casing and gives methis.username = data["Username"];
and you end up withundefined
for all the properties.This is with all the latest bits from today using msbuild targets to generate the code. I haven't used nswag in a couple months, so can't say exactly when this started.
// Generated using the NSwag toolchain v11.17.19.0 (NJsonSchema v9.10.58.0 (Newtonsoft.Json v9.0.0.0)) (http://NSwag.org)