swagger-api / swagger-api/swagger-codegen

importMapping functionality lacking core functionality, such as rename ability.

Open
#5,881 3 comments 2 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

This issue I have found for <language>spring</language> but I'm sure other languages are effected as well.
As an example: In Spring, it is common to use pagination for returning a collection of resources, usually one uses the org.springframework.data.domain.Page class in REST APIs. I want to use a importMapping for this situation. However one cannot do this if they have more than one type of collection in the same yaml file because Swagger Code Gen seems to have only minimal functionality. For instance say there is ResourceA and ResourceB, giving PageA and PageB:

    PageA:
      description: Pagination response returned
      type: object
      properties:
        content:
          type: array
          items:
              $ref: "#/definitions/ResourceA"
        last:
          type: boolean
          description: flag to mark if this is the last response
        totalElements:
          type: integer
          description: the total number of elements
        totalPages:
          type: integer
          description: the total number of pages
        size:
          type: integer
          description: not sure what this is
        number:
          type: integer
          description: the current page
        sort:
          type: string
          description: the field sorted by
        first:
          type: boolean
          description: flag to mark if this is the first page
        numberOfElements:
          type: string
          description: the number of matches in this page
		  
		  
    PageB:
      description: Pagination response returned
      type: object
      properties:
        content:
          type: array
          items:
              $ref: "#/definitions/ResourceB"
		......... EVERYTHING ELSE THE SAME AS PAGE A

There is no way you can then use an importMapping for both these classes because of the constraint that <import-mappings>PageA=org.springframework.data.domain.Page</import-mappings> does not rename PageA to org.springframework.data.domain.Page. In fact there is not even an import org.springframework.data.domain.Page produced by the api.mustache file. Whilst for the latter there is a work around for just adding the import statement manually in the api.mustache file in the case for Spring. There is no way to replace both PageA and PageB with org.springframework.data.domain.Page. If you only have one type of collection in a yaml file this is not so much of an issue as you can simply rename PageA to Page. As a general comment I find the importMapping functionality lacking as all it seems to do is cancel the generation of a PageA.java and PageB.java file, and nothing else. If that is its intended use it would make more sense to call it something else as importMapping I feel gives the impression you will map a resource type to an existing class yet it all it does is stop the generation of a file and leave you with a class path error...

Swagger-codegen version

2.2.2

Swagger declaration file content or url
Steps to reproduce

<import-mappings>PageA=org.springframework.data.domain.Page;Page=org.springframework.data.domain.PageB;</import-mappings>

Related issues
Suggest a Fix

IMHO at least for Java the importMapping statement should be deprecated in favour of a solution for really mapping what would be a generate swagger class to an existing class which I thought was its intended purpose. As it stands I achieve the same functionality just by deleting the class it produces. As a note I think this functionality is very important for the adoption of swagger code-gen, as a Java Dev whilst the API description is extremely useful, I can't see that greater use for the code-gen in it's current state, other than for generating a template for a new project.

As another suggestion although not needed fully, generics would be great to have as well for instance if I could do <import-mappings>PageA=org.springframework.data.domain.Page<com.something.ResourceA></import-mappings> or my own custom implementation of ResourceA or <import-mappings>PageA=org.springframework.data.domain.Page<ResourceA></import-mappings> for a swagger generation of ResourceA. (But this is not core only a nice to have).

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 by reproducing the PageA/PageB import-mappings example for the Spring generator and inspect the api.mustache template mentioned in the report. Compare the generated files and imports with the requested mapping behavior; done means existing Spring Page types are referenced correctly without generating PageA.java or PageB.java.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.