C
C#3d ago
khamas

nswag (OpenApi) typescript get error type

Hey, I'm generating an api client using nswag, here is an example route:
[HttpPost("BeginLogin")]
[ProducesResponseType(typeof(AppError), 400)]
[ProducesResponseType(typeof(BeginLoginResponse), 200)]
public async Task<ActionResult> BeginLogin([FromBody] BeginLoginRequest request)
[HttpPost("BeginLogin")]
[ProducesResponseType(typeof(AppError), 400)]
[ProducesResponseType(typeof(BeginLoginResponse), 200)]
public async Task<ActionResult> BeginLogin([FromBody] BeginLoginRequest request)
However, in the client itself the response is not a union type, but simply the 200 default type... How am I supposed to get the error in the frontend (typescript)?
beginLogin(authorization: any | undefined, request: BeginLoginRequest): Promise<BeginLoginResponse>{}
beginLogin(authorization: any | undefined, request: BeginLoginRequest): Promise<BeginLoginResponse>{}
11 Replies
Unknown User
Unknown User3d ago
Message Not Public
Sign In & Join Server To View
Sehra
Sehra3d ago
400 is not a success code, so AppError will be signal as exception
khamas
khamasOP3d ago
right but 201 is also a generated exception
Sehra
Sehra3d ago
looked more at it and nswag merges 2xx responses if they are the same type, others throw
khamas
khamasOP3d ago
well it didn't seem to merge them together I tried with multiple types on 200
Sehra
Sehra3d ago
[JsonDerivedType(typeof(WeatherForecast), "base")]
[JsonDerivedType(typeof(WeatherForecastCity), "city")]
public class WeatherForecast
{
...
}

public class WeatherForecastCity : WeatherForecast
{
...
}
[JsonDerivedType(typeof(WeatherForecast), "base")]
[JsonDerivedType(typeof(WeatherForecastCity), "city")]
public class WeatherForecast
{
...
}

public class WeatherForecastCity : WeatherForecast
{
...
}
[HttpGet(Name = "GetWeatherForecast")]
[ProducesResponseType<IEnumerable<WeatherForecast>>(200)]
[ProducesResponseType<IEnumerable<WeatherForecast>>(201)]
public IEnumerable<WeatherForecast> Get()
{
...
}
[HttpGet(Name = "GetWeatherForecast")]
[ProducesResponseType<IEnumerable<WeatherForecast>>(200)]
[ProducesResponseType<IEnumerable<WeatherForecast>>(201)]
public IEnumerable<WeatherForecast> Get()
{
...
}
protected processGetWeatherForecast(response: Response): Promise<(WeatherForecastWeatherForecast | WeatherForecastWeatherForecastCity)[]> {
const status = response.status;
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
if (status === 200) {
return response.text().then((_responseText) => {
let result200: any = null;
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as (WeatherForecastWeatherForecast | WeatherForecastWeatherForecastCity)[];
return result200;
});
} else if (status === 201) {
return response.text().then((_responseText) => {
let result201: any = null;
result201 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as (WeatherForecastWeatherForecast | WeatherForecastWeatherForecastCity)[];
return result201;
});
} else if (status !== 200 && status !== 204) {
return response.text().then((_responseText) => {
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
});
}
return Promise.resolve<(WeatherForecastWeatherForecast | WeatherForecastWeatherForecastCity)[]>(null as any);
}
protected processGetWeatherForecast(response: Response): Promise<(WeatherForecastWeatherForecast | WeatherForecastWeatherForecastCity)[]> {
const status = response.status;
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
if (status === 200) {
return response.text().then((_responseText) => {
let result200: any = null;
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as (WeatherForecastWeatherForecast | WeatherForecastWeatherForecastCity)[];
return result200;
});
} else if (status === 201) {
return response.text().then((_responseText) => {
let result201: any = null;
result201 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as (WeatherForecastWeatherForecast | WeatherForecastWeatherForecastCity)[];
return result201;
});
} else if (status !== 200 && status !== 204) {
return response.text().then((_responseText) => {
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
});
}
return Promise.resolve<(WeatherForecastWeatherForecast | WeatherForecastWeatherForecastCity)[]>(null as any);
}
khamas
khamasOP3d ago
right but that is inheritance I was talking about unrelated types also for some reason I can't instantiate request dtos using like <BeginLoginRequest>{ email: "[email protected]" }; I can live with single response types for 200
Sehra
Sehra3d ago
first 2xx is primary success response type, another other 2xx can only use same type. other types will be thrown as exception
khamas
khamasOP3d ago
@Sehra , do you know how to instantiate the type using <BeginLoginRequest>{ email: "[email protected]" } it just shows me an error when i do this and svelte gives 500 error I also need to generate a base class to inject headers in the request nvm I found it
let resp = await new AuthenticationClient()
.beginLogin(new BeginLoginRequest({
latitude: 0,
longitude: 0,
preferredLanguage: "EN",
phoneNumber: "0123456789"
}));
let resp = await new AuthenticationClient()
.beginLogin(new BeginLoginRequest({
latitude: 0,
longitude: 0,
preferredLanguage: "EN",
phoneNumber: "0123456789"
}));
MODiX
MODiX3d ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?