0

软件的总体理念nswag是惊人的。

伙计们已经完全毁了它。

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

  • 过于复杂

  • 有问题的

  • 记录极差

  • 不受欢迎

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

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

在此处输入图像描述

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

nswag不再生成输入类(我的意思是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"
    },
4

1 回答 1

0

这解决了我在上面的帖子中提到的 URL 问题。

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

例如

public async Task<VocabularyDto> ImportEntries([FromBody] ImportEntriesInput input)
于 2018-07-09T05:06:13.670 回答