-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[BUG] [typescript-fetch] Request Parameter Interfaces Conflict with remove-operation-id-prefix #1998
Comments
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
This conflict also happens when I already have a model definition that is accidentally named |
^ I was just constructing a repro case to file a fresh issue for what @Teyras mentioned above. We have some model definitions named Request by design, and that makes the typescript-fetch generation unusable for us (we're trying to move off of swagger-codegen and this blocks us). |
Repro case: example_spec.yaml swagger: "2.0"
info:
title: "Example Clash"
version: "1.0"
schemes:
- https
host: http://localhost
consumes:
- application/json
produces:
- application/json
paths:
/v1/foos:
post:
operationId: CreateFoo
responses:
"200":
description: A successful response.
schema:
$ref: '#/definitions/Foo'
parameters:
- name: body
in: body
required: true
schema:
$ref: '#/definitions/CreateFooRequest'
tags:
- Example
definitions:
CreateFooRequest:
type: object
properties:
nickname:
type: string
required:
- nickname
Foo:
type: object
properties:
id:
type: string
nickname:
type: string
required:
- id
- nickname Snippet from generated src/api.ts: import {
CreateFooRequest,
CreateFooRequestFromJSON,
CreateFooRequestToJSON,
Foo,
FooFromJSON,
FooToJSON,
} from '../models';
export interface CreateFooRequest {
body: CreateFooRequest;
}
/**
* no description
*/
export class ExampleApi extends runtime.BaseAPI {
/**
*/
async createFooRaw(requestParameters: CreateFooRequest): Promise<runtime.ApiResponse<Foo>> {
if (requestParameters.body === null || requestParameters.body === undefined) {
throw new runtime.RequiredError('body','Required parameter requestParameters.body was null or undefined when calling createFoo.');
}
const queryParameters: runtime.HTTPQuery = {};
const headerParameters: runtime.HTTPHeaders = {};
headerParameters['Content-Type'] = 'application/json';
const response = await this.request({
path: `/v1/foos`,
method: 'POST',
headers: headerParameters,
query: queryParameters,
body: CreateFooRequestToJSON(requestParameters.body),
});
return new runtime.JSONApiResponse(response, (jsonValue) => FooFromJSON(jsonValue));
}
/**
*/
async createFoo(requestParameters: CreateFooRequest): Promise<Foo> {
const response = await this.createFooRaw(requestParameters);
return await response.value();
}
} (see that we both import and declare the type The generated code does not compile:
|
Fix OpenAPITools#1998 by namespacing the generated request object types used in the typescript-fetch client codegen.
#3695 is the start of a fix along the lines of the namespacing suggestion from @bradenmacdonald (which fixes both of the naming collision issues discussed in this thread). Note that adding namespacing can be a breaking change for clients of the generated code. So that this does not have to wait for a major release (based on policies in https://github.com/OpenAPITools/openapi-generator/wiki/Git-Branches ), I think I should add an option to control this behavior? |
* [typescript-fetch] Fix #1998: namespace reqs. Fix #1998 by namespacing the generated request object types used in the typescript-fetch client codegen. * Update petstore samples. * Add default-false namespacing config option. * Re-run code sample generation. * Add maven build configurations. * Fix sample VERSION files. * Update typescript-fetch docs. * Emulate with-npm-version, adding package.json. * Add Paxos to companies using. * Prefix namespace with basename to avoid collisions.
Updated: change the namespacing flag to prefixing flag. Starting with release 4.1.1 (which should be out soon), you can avoid this issue by passing
on the command line with your Or, adding |
So... @macjohnny @bradenmacdonald I discovered while trying to incorporate the new generated code into my team's build today that Typescript namespaces are not well-supported by Babel (often used in React projects like ours). I think the specific usage of namespaces added here actually would work with Babel after a slight tweak to add All of this could probably be worked around with some creative tooling setup, but I wonder if it would be better to abandon namespaces entirely and instead do ugly but tooling-friendly string prefixing/suffixing (e.g. I bring this up now because if we don't want to use namespaces here, it would be good not to release the current (new) behavior in 4.1.1. |
@jgiles thabks for the analysis, go ahead. It should be as easy as removing the dot, right? |
Yeah it's simpler actually than the namespacing was. How close is the 4.1.1 release? I can try to make the change tomorrow. |
@wing328 can we wait for this change before releasing 4.1.1? |
Prefix generated request interface names with API classname rather than using namespaces, because namespaces are not well-supported by some tooling (in particular, Babel).
Prefix generated request interface names with API classname rather than using namespaces, because namespaces are not well-supported by some tooling (in particular, Babel).
* master: (45 commits) update python samples Two tiny fixes for Java Vertx client (#3683) Exclude "test" from packages so that it is not installed as top-level package when "pip installing" the generated sdk (#3731) [java-client] add unit test for JDK 11 HttpClient (#3735) [R] feat(r): handling error response for 3xx cases (#3571) [Dart] Avoid redundant null checking in fromJson (#3632) [core] Set isMultipart=true for multipart operations, fix possible NPE (#3750) reenable kotlin client tests in shippable ci (#3751) Fix null assignation to header/query param map (#3749) Mockito 3.0.0 (#3732) Update to swagger-parser to 2.0.14-OpenAPITools.org-1 (#3737) Add a link to a Qiita blog post (#3747) update csharp samples Simple spelling error in dotnet core comments (#3746) better support for multipart (#3736) [typescript-fetch] fix #1998: prefix req ifaces. (#3740) cpp-qt5-client: add valgrind memory test (#3663) [typescript-angular] Encode objects for multipart/form-data with json - fixes #2733 (#3738) [BUG] Https protocol schema is not handled correctly in java 11 native code #3729 (#3730) comment out kotlin client tests ...
Bug Report Checklist
Description
When an API spec contains two similarly named operations that differ only in prefix:
then generating the
typescript-fetch
client with the--remove-operation-id-prefix
option will result in an error like:openapi-generator version
This is in 4.0.0beta. The same error did not occur with the old 3.x typescript-fetch, though this new implementation is generally much better.
OpenAPI declaration file content or url
https://gist.github.com/bradenmacdonald/061c37b42af22832a14574e4aeaa5c59
Command line used for generation
Steps to reproduce
Simply generate the typescript-fetch client using the sample gist and the command above, then run
cd api_client; npm install; npm run build
Related issues/PRs
I couldn't find any existing reports of this issue.
Suggest a fix
Ideally just never omit the prefixes when generating the request parameter interfaces, so that the interface is still called e.g.
TagNewCreateRequest
(instead ofinterface NewCreateRequest
) regardless of the--remove-operation-id-prefix
option (the main reason I use that option is for nicer method names; the names of the parameter interfaces aren't often used directly so it's fine if they're more verbose).Alternately, put each module's interfaces into a namespace for that module, so that one must use e.g.
TagApi.NewCreateRequest
to get the interface after importing the overall API.The text was updated successfully, but these errors were encountered: