nswag 生成破坏 URL 的代理

nswag generates proxy which spoils the URL

nswag 软件的总体思路非常棒。

虽然这些家伙完全毁了它。

我现在真的在考虑放弃它,原因如下:

关于我的版本 - "nswag@11.17.19"

我的服务应该传递复合结构(例如嵌套数组)- 但在最近的版本中,它通过 URL 传递所有内容,这就是我的意思:

此外,它的最新版本不会生成输入 classes - 例如我的 API 控制器有动作 ImportEntries(ImportEntriesInput input)

nswag 不再生成输入 class(我的意思是 ImportEntriesInput)——相反它只列出其所有成员:

例如比较

importEntries(input: ImportEntriesInput | null | undefined): Observable<VocabularyDto> {

importEntries(entries: CrawlerEntryDto[] | null | undefined, vocabularyId: number | undefined, newVocabulary: boolean | undefined, typeId: number | undefined, name: string | null | undefined, notes: string | null | undefined): Observable<VocabularyDto | null> {

也许开发它的人觉得它没问题,但我想说这完全使整个方法过于复杂并且太糟糕了

我真的找不到涵盖这部分的文档。

有人知道怎么解决吗?


此外,这是它创建正在传递的内容的位 URL:

importEntries(entries: CrawlerEntryDto[] | null | undefined, vocabularyId: number | undefined, newVocabulary: boolean | undefined, typeId: number | undefined, name: string | null | undefined, notes: string | null | undefined): Observable<VocabularyDto | null> {
    let url_ = this.baseUrl + "/api/Import/ImportEntries?";
    if (entries !== undefined)
        entries && entries.forEach((item, index) => { 
            for (let attr in item)
                url_ += "entries[" + index + "]." + attr + "=" + encodeURIComponent("" + item[attr]) + "&";
        });
    if (vocabularyId === null)
        throw new Error("The parameter 'vocabularyId' cannot be null.");
    else if (vocabularyId !== undefined)
        url_ += "vocabularyId=" + encodeURIComponent("" + vocabularyId) + "&"; 
    if (newVocabulary === null)
        throw new Error("The parameter 'newVocabulary' cannot be null.");
    else if (newVocabulary !== undefined)
        url_ += "newVocabulary=" + encodeURIComponent("" + newVocabulary) + "&"; 
    if (typeId === null)
        throw new Error("The parameter 'typeId' cannot be null.");
    else if (typeId !== undefined)
        url_ += "typeId=" + encodeURIComponent("" + typeId) + "&"; 
    if (name !== undefined)
        url_ += "name=" + encodeURIComponent("" + name) + "&"; 
    if (notes !== undefined)
        url_ += "notes=" + encodeURIComponent("" + notes) + "&"; 
    url_ = url_.replace(/[?&]$/, "");

    let options_ : any = {
        observe: "response",
        responseType: "blob",
        headers: new HttpHeaders({
            "Content-Type": "application/json", 
            "Accept": "application/json",
            'Authorization': 'Bearer ' + localStorage.getItem('token')
        })
    };

    return this.http.request("post", url_, options_).flatMap((response_ : any) => {
        return this.processImportEntries(response_);
    }).catch((response_: any) => {
        if (response_ instanceof HttpResponseBase) {
            try {
                return this.processImportEntries(<any>response_);
            } catch (e) {
                return <Observable<VocabularyDto | null>><any>Observable.throw(e);
            }
        } else
            return <Observable<VocabularyDto | null>><any>Observable.throw(response_);
    });
}

非常可怕,不是吗?

swaggerToTypeScriptClient 来自配置的位:

"codeGenerators": {
    "swaggerToTypeScriptClient": {
      "className": "{controller}ServiceProxy",
      "moduleName": "",
      "namespace": "",
      "typeScriptVersion": 2.0,
      "template": "Angular",
      "promiseType": "Promise",
        "httpClass": "HttpClient",
      "dateTimeType": "MomentJS",
      "nullValue": "Undefined",
      "generateClientClasses": true,
      "generateClientInterfaces": false,
      "generateOptionalParameters": false,
      "wrapDtoExceptions": false,
      "wrapResponses": false,
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "useTransformOptionsMethod": false,
      "useTransformResultMethod": false,
      "generateDtoTypes": true,
      "operationGenerationMode": "MultipleClientsFromPathSegments"
      "markOptionalProperties": false,
      "generateCloneMethod": true,
      "typeStyle": "Class",
      "extensionCode": "service.extensions.ts",
      "generateDefaultValues": true,
      "excludedTypeNames": [],
      "handleReferences": false,
      "generateConstructorInterface": true,
      "importRequiredTypes": true,
      "useGetBaseUrlMethod": false,
      "baseUrlTokenName": "API_BASE_URL",
      "injectionTokenType": "InjectionToken",
      "output": "../src/shared/service-proxies/service-proxies.ts"
    },

这解决了我上面 post 中提到的 URL 的问题。

这没有记录在案,但为了 nswag 与 ASP.NET Core 一起正常工作,您应该将 [FromBody] 属性应用于接受数据的每个操作

例如

public async Task<VocabularyDto> ImportEntries([FromBody] ImportEntriesInput input)