nswag 生成的客户端响应类型错误

nswag generated client with wrong response type

这是在 nswag swagger 客户端生成的帮助下从 C# 控制器自动生成的 Angular 服务文件。下面的代码具有所有服务。在组件中,我试图调用 getCompanyInfoOption() api。但是我没有收到我从控制器发送的响应数据。

/* tslint:disable */
/* eslint-disable */
//----------------------
// <auto-generated>
//     Generated using the NSwag toolchain v13.6.0.0 (NJsonSchema v10.1.18.0 (Newtonsoft.Json v12.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------
// ReSharper disable InconsistentNaming

import { mergeMap as _observableMergeMap, catchError as _observableCatch } from 'rxjs/operators';
import { Observable, throwError as _observableThrow, of as _observableOf } from 'rxjs';
import { Injectable, Inject, Optional, InjectionToken } from '@angular/core';
import { HttpClient, HttpHeaders, HttpResponse, HttpResponseBase } from '@angular/common/http';

export const API_BASE_URL = new InjectionToken<string>('API_BASE_URL');

@Injectable({
providedIn:'root'
})
export class CompanyInfoService {
    private http: HttpClient;
    private baseUrl: string;
    protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined;

    constructor(@Inject(HttpClient) http: HttpClient, @Optional() @Inject(API_BASE_URL) baseUrl?: string) {
        this.http = http;
        this.baseUrl = baseUrl ? baseUrl : "https://localhost:44311";
    }

    getCompanyInfoOption(): Observable<FileResponse | null> {
        let url_ = this.baseUrl + "/api/company-info";
        url_ = url_.replace(/[?&]$/, "");

        let options_ : any = {
            observe: "response",
            responseType: "blob",
            headers: new HttpHeaders({
                "Accept": "application/octet-stream"
            })
        };

        return this.http.request("get", url_, options_).pipe(_observableMergeMap((response_ : any) => {
            return this.processGetCompanyInfoOption(response_);
        })).pipe(_observableCatch((response_: any) => {
            if (response_ instanceof HttpResponseBase) {
                try {
                    return this.processGetCompanyInfoOption(<any>response_);
                } catch (e) {
                    return <Observable<FileResponse | null>><any>_observableThrow(e);
                }
            } else
                return <Observable<FileResponse | null>><any>_observableThrow(response_);
        }));
    }

    protected processGetCompanyInfoOption(response: HttpResponseBase): Observable<FileResponse | null> {
        const status = response.status;
        const responseBlob =
            response instanceof HttpResponse ? response.body :
            (<any>response).error instanceof Blob ? (<any>response).error : undefined;

        let _headers: any = {}; if (response.headers) { for (let key of response.headers.keys()) { _headers[key] = response.headers.get(key); }}
        if (status === 200 || status === 206) {
            const contentDisposition = response.headers ? response.headers.get("content-disposition") : undefined;
            const fileNameMatch = contentDisposition ? /filename="?([^"]*?)"?(;|$)/g.exec(contentDisposition) : undefined;
            const fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[1] : undefined;
            return _observableOf({ fileName: fileName, data: <any>responseBlob, status: status, headers: _headers });
        } else if (status !== 200 && status !== 204) {
            return blobToText(responseBlob).pipe(_observableMergeMap(_responseText => {
            return throwException("An unexpected server error occurred.", status, _responseText, _headers);
            }));
        }
        return _observableOf<FileResponse | null>(<any>null);
    }

如何在我的组件中调用此服务?我正在尝试在组件

的 ngOnInit 中使用 subscribe 方法调用
  ngOnInit(): void {
      this.service.getCompanyInfoOption().subscribe(
        value => {
          console.log(value);
        },
        error => console.error(JSON.stringify(error)),
        () => console.log('done')
      );
  }

我在调用 getCompanyInfoOption 后得到这个输出 api

Console Log

我的 C# 控制器

ASP .net core C# controller

compantInfoOption send in ok的响应类型如下

conpanyInfoOption type

看来你是。因为您的响应类型是 FileResponse 并且在 console.log 中您可能会看到此对象。其他在数据中存储的东西 blob 如果你需要文本解释,如果它只是使用类似 blobToText 的函数,它位于你生成的文件的末尾。

如果我错了,请在问题中添加期望结果。

我猜生成的代码有问题,因为你的控制器 return 类型不正确,尝试使用 ActionResult,然后生成的 api 将 return 正确的结果而不是期望文件响应。

你的情况

public async Task<ActionResult<CompanyInfoOptionAc>> GetCompanyInfo(){
//etc
}