swagger-api / swagger-api/swagger-codegen

[JAVA] [jersey1] [jersey2] Response class collision

Open
#8,774 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When the abstract API class get generated if I have a "Response" object in my model defined at yaml file, the imports will collide with javax.ws.rs.core.Response. Using "modelNameSuffix" configuration can workaround this however every class model get an odd name.
Many times you can't control the model names as it is from some partner.

Swagger-codegen version: 2.3.1
Swagger declaration file content or url
swagger: '2.0'
info:
  version: '0.1'
  title: ColllisionEG
host: localhost:9080
basePath: /collision
tags:
  - name: test
    description: example
schemes:
  - http
paths:
  /services/test:
    get:
      tags:
        - test
      summary: test for bug
      description: >-
        collision example
      operationId: test
      produces:
        - application/json
      responses:
        '200':
          description: >-
          schema:
            $ref: '#/definitions/response'
          examples:
            COLLISION:
              collision: "collided"
        '201':
          description: >- 
          schema:
            $ref: '#/definitions/response'
          examples:
           COLLISION:
              collision: "collided"
definitions:
  response:
    type: object
    required:
      - someParam
    properties:
      someParam:
        type: string
        maxLength: 16
        description: Example
Command line used for generation

In the case I'm using maven plugin so:

<plugin>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>${project.basedir}/src/main/resources/my.yaml</inputSpec>
        <language>jaxrs</language>
        <library>jersey1</library>
        <apiPackage>com.api</apiPackage>
        <modelPackage>com.model</modelPackage>
        <invokerPackage>com.invoker</invokerPackage>
        <modelNameSuffix>Model</modelNameSuffix>
        <generateApis>true</generateApis>
        <generateModels>true</generateModels>
        <generateSupportingFiles>true</generateSupportingFiles>
        <addCompileSourceRoot>true</addCompileSourceRoot>
        <ignoreFileOverride>${project.basedir}/.swagger-codegen-ignore</ignoreFileOverride>
        <configOptions>
          <sourceFolder>src/gen/java</sourceFolder>
        </configOptions>
      </configuration>
    </execution>
  </executions>
</plugin>
Steps to reproduce n/a
Related issues/PRs: none
Suggest a fix/enhancement

My suggestion is to generate the abstract API methods with the fully qualified name of the class and don't import it as below that would avoid any collision.

public abstract class ServicesApiService {
      public abstract javax.ws.rs.core.Response apiCall( @Size(max=320)String param1,Entitity1 body, SecurityContext securityContext)
      throws NotFoundException;
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Maven generation configuration using language jaxrs and the jersey1 library, then generate the supplied Swagger definition and inspect ServicesApiService alongside the Response model. Compare the jersey1 and jersey2 output; done means a model named Response no longer causes an import collision and the generated Java sources compile.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.