OpenAPITools / OpenAPITools/openapi-generator
[BUG] Generator generates weird code from swagger spec
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
swagger.json
"/templates/{id}": {
"get": {
"description": "Get a template",
"produces": [
"application/json"
],
"tags": [
"Templates"
],
"summary": "Get a template",
"parameters": [
{
"type": "integer",
"description": "Template ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "Template retrieved",
"schema": {
"$ref": "#/definitions/Template"
}
},
"404": {
"description": "Template not found",
"schema": {
"$ref": "#/definitions/PublicHTTPError"
}
},
"500": {
"description": "Internal server error",
"schema": {
"$ref": "#/definitions/PublicHTTPError"
}
}
}
},
and from this a models file is generated called TemplatesIdGetRequest.ts
TemplatesIdGetRequest.ts
import { exists, mapValues } from '../runtime';
/**
*
* @export
* @interface TemplatesIdGetRequest
*/
export interface TemplatesIdGetRequest {
/**
*
* @type {string}
* @memberof TemplatesIdGetRequest
*/
html: string;
/**
*
* @type {string}
* @memberof TemplatesIdGetRequest
*/
name: string;
}
/**
* Check if a given object implements the TemplatesIdGetRequest interface.
*/
export function instanceOfTemplatesIdGetRequest(value: object): boolean {
let isInstance = true;
isInstance = isInstance && "html" in value;
isInstance = isInstance && "name" in value;
return isInstance;
}
export function TemplatesIdGetRequestFromJSON(json: any): TemplatesIdGetRequest {
return TemplatesIdGetRequestFromJSONTyped(json, false);
}
export function TemplatesIdGetRequestFromJSONTyped(json: any, ignoreDiscriminator: boolean): TemplatesIdGetRequest {
if ((json === undefined) || (json === null)) {
return json;
}
return {
'html': json['html'],
'name': json['name'],
};
}
export function TemplatesIdGetRequestToJSON(value?: TemplatesIdGetRequest | null): any {
if (value === undefined) {
return undefined;
}
if (value === null) {
return null;
}
return {
'html': value.html,
'name': value.name,
};
}
and then the code is being used like this:
/**
* Get a template
* Get a template
*/
async templatesIdGet(requestParameters: TemplatesIdGetRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<Template> {
const response = await this.templatesIdGetRaw(requestParameters, initOverrides);
return await response.value();
}
/**
* Get a template
* Get a template
*/
async templatesIdGetRaw(requestParameters: TemplatesIdGetRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<Template>> {
if (requestParameters.id === null || requestParameters.id === undefined) { <-- here is where the error happens: the dto does not contain an id property
throw new runtime.RequiredError('id','Required parameter requestParameters.id was null or undefined when calling templatesIdGet.');
}
const queryParameters: any = {};
const headerParameters: runtime.HTTPHeaders = {};
const response = await this.request({
path: `/templates/{id}`.replace(`{${"id"}}`, encodeURIComponent(String(requestParameters.id))),
method: 'GET',
headers: headerParameters,
query: queryParameters,
}, initOverrides);
return new runtime.JSONApiResponse(response, (jsonValue) => TemplateFromJSON(jsonValue));
}
This is my generate command:
openapi-generator-cli generate -i http://server.me/swagger.json -g typescript-fetch -o src/ogen
The problem is that the generated code is not like in the swagger spec json described hence why my code doesn't compile.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the swagger.json excerpt and the generated TemplatesIdGetRequest.ts, then inspect the templatesIdGetRaw entry point produced by the typescript-fetch generator. Reproduce the issue with the shown openapi-generator-cli command and compare the generated request model with the path parameter; done means the generated TypeScript reflects the specification and compiles.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100