NsWag generated code for c# dose not match with response dto object
See original GitHub issueI use blow code for get token in winform:
var tokenService = new Services.WebApi.TokenAuthClient("http://localhost:21021");
var result = await tokenService.AuthenticateAsync(new Services.WebApi.AuthenticateModel()
{
UserNameOrEmailAddress = "admin",
Password = "123qwe",
RememberClient = true
});
The service class generated via Nswag. but problem where blow method want deserialize json response to AuthenticateResultModel and it not work.
public async System.Threading.Tasks.Task<AuthenticateResultModel> AuthenticateAsync(AuthenticateModel model, System.Threading.CancellationToken cancellationToken)
{
...
result_ = Newtonsoft.Json.JsonConvert.DeserializeObject<AuthenticateResultModel>(responseData_ , _settings.Value);
....
}
deserializer not work because response template do not match with AuthenticateResultModel Response:
{
"result":
{
"accessToken":"5",
"encryptedAccessToken":"6",
"expireInSeconds":86400,
"userId":2
},
"targetUrl":null,
"success":true,
"error":null,
"unAuthorizedRequest":false,
"__abp":true
}
But AuthenticateResultModel:
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "9.6.5.0 (Newtonsoft.Json v9.0.0.0)")]
public partial class AuthenticateResultModel
{
[Newtonsoft.Json.JsonProperty("accessToken", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string AccessToken { get; set; }
[Newtonsoft.Json.JsonProperty("encryptedAccessToken", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string EncryptedAccessToken { get; set; }
[Newtonsoft.Json.JsonProperty("expireInSeconds", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public int? ExpireInSeconds { get; set; }
[Newtonsoft.Json.JsonProperty("userId", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public long? UserId { get; set; }
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this);
}
public static AuthenticateResultModel FromJson(string data)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<AuthenticateResultModel>(data);
}
}
I test with new model class and it work how configure Nswag or Abp to generate correct code?
public class AuthenticateResultModel
{
public AuthenticateResultModel Result{ get; set; }
public string TargetUrl { get; set; }
public bool Success { get; set; }
public string Error { get; set; }
public bool UnAuthorizedRequest { get; set; }
public bool __abp { get; set; }
}
public class AuthenticateResultModel
{
public string AccessToken { get; set; }
public string EncryptedAccessToken { get; set; }
public int? ExpireInSeconds { get; set; }
public long? UserId { get; set; }
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How do I customise the generated DTO for the C# Client in ...
NET's HttpClient but I'd like to use NSwagStudio's code generator to generate code that calls my generic client rather than the standard ...
Read more >Whats your preferred way to create a typed C# client from ...
NSwag provides a host of really good options for generating clients from swagger docs. This is what we use for a similar case...
Read more >Generate C# client for OpenAPI - Kaylumah
So I needed a type-safe client for use in my C# code base. ... the name of the MSBuild variable does not need...
Read more >Get started with NSwag and ASP.NET Core
Learn how to use NSwag to generate documentation and help pages for an ASP.NET Core web API.
Read more >Generate TypeScript and C# clients with NSwag based on ...
Learn how to generate TypeScript and C# API clients with NSwag to reduce your workload when building a project.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This worked for me, but I used
[ProducesResponseType(typeof(MyResponse<...>), 200)]
instead of
[SwaggerResponse(200, typeof(MyResponse<...>)]
as per this
Very simple solution: