RicoSuter/NSwag
View on GitHubGenerating from a swagger.json with no endpoints creates `throwException` method, but doesn't create `SwaggerException` class.
Open
#1610 opened on Sep 14, 2018
help wantedtype: bug
Description
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.