RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.74k stars 1.29k forks source link

Generating from a swagger.json with no endpoints creates `throwException` method, but doesn't create `SwaggerException` class. #1610

Open ascott18 opened 6 years ago

ascott18 commented 6 years ago

A bit of an edge case - in a project I'm working on, we integrated automatic generation of clients via some MSBuild targets that are automatically pulled in via a shared nuget package. This is all working great, except at the very beginning of a new project where we haven't written any endpoints yet, and are still working on the data/business layer of the app. At this stage, our swagger.json has no endpoints defined.

The TypeScript that gets generated doesn't compile because SwaggerException isn't created unless there is at least one client.

For a swagger.json:

{"swagger":"2.0","info":{"version":"v1","title":"My API"},"basePath":"/my-api","paths":{},"definitions":{}}

and spec:

{
  "runtime": "Default",
  "defaultVariables": null,
  "swaggerGenerator": {
    "fromSwagger": {
      "url": "C:/local/path/to/swagger.json",
      "output": null
    }
  },
  "codeGenerators": {
    "swaggerToTypeScriptClient": {
      "className": "{controller}Client",
      "moduleName": "",
      "namespace": "",
      "typeScriptVersion": 2.7,
      "template": "Fetch",
      "promiseType": "Promise",
      "httpClass": "Http",
      "useSingletonProvider": false,
      "injectionTokenType": "OpaqueToken",
      "rxJsVersion": 5.0,
      "dateTimeType": "Date",
      "nullValue": "Null",
      "generateClientClasses": true,
      "generateClientInterfaces": false,
      "generateOptionalParameters": true,
      "exportTypes": true,
      "wrapDtoExceptions": true,
      "clientBaseClass": "BaseClient",
      "wrapResponses": true,
      "wrapResponseMethods": [],
      "generateResponseClasses": false,
      "responseClass": "SwaggerResponse",
      "protectedMethods": [],
      "configurationClass": "ClientConfig | undefined = undefined",
      "useTransformOptionsMethod": true,
      "useTransformResultMethod": true,
      "generateDtoTypes": true,
      "operationGenerationMode": "MultipleClientsFromOperationId",
      "markOptionalProperties": true,
      "generateCloneMethod": false,
      "typeStyle": "Interface",
      "classTypes": [],
      "extendedClasses": [],
      "extensionCode": "injected.ts",
      "generateDefaultValues": true,
      "excludedTypeNames": [],
      "handleReferences": false,
      "generateConstructorInterface": true,
      "convertConstructorInterfaceData": false,
      "importRequiredTypes": true,
      "useGetBaseUrlMethod": true,
      "baseUrlTokenName": "API_BASE_URL",
      "queryNullValue": "",
      "templateDirectory": null,
      "typeNameGeneratorType": null,
      "propertyNameGeneratorType": null,
      "enumNameGeneratorType": null,
      "serviceHost": null,
      "serviceSchemes": null,
      "output": "index.ts"
    }
  }
}

We get the following output:

/* tslint:disable */
//----------------------
// <auto-generated>
//     Generated using the NSwag toolchain v11.19.2.0 (NJsonSchema v9.10.73.0 (Newtonsoft.Json v9.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------
// ReSharper disable InconsistentNaming

import { BaseClient, ClientConfig, SwaggerResponse } from '@org/our-base-code'

function throwException(message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): any {
    throw new SwaggerException(message, status, response, headers, result);
}

See that throwException is being generated, but SwaggerException is not, so the TypeScript can't compile.

marco-gallinari commented 5 years ago

we're on the same issue. Using a generator to create types for a npm angular library with a conf like this:


{
  "runtime": "NetCore20",
  "swaggerGenerator": {
    "typesToSwagger": {
      "output": "swagger.json",
      "assemblyPaths": [ "$(OutDir)my.dll" ],
      "classNames": [
        "myclass"
      ]
    }
  },
  "codeGenerators": {
    "swaggerToTypeScriptClient": {
      "wrapDtoExceptions": false,
      "input": "swagger.json",
      "output": "servertypes.ts"
    }
  }
}

generates this:

/* tslint:disable */
//----------------------
// <auto-generated>
//     Generated using the NSwag toolchain v11.17.6.0 (NJsonSchema v9.10.46.0 (Newtonsoft.Json v9.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------
// ReSharper disable InconsistentNaming

export class myclass implements Imyclass {
    ...
}

export interface Imyclass {
    ...
}

function throwException(message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): any {
    if(result !== null && result !== undefined)
        throw result;
    else
        throw new SwaggerException(message, status, response, headers, null);
}

the output works fine, as soon as we manually remove throwException from the file.

RicoSuter commented 5 years ago

I think this is already fixed with this PR: https://github.com/RSuter/NSwag/pull/1705