swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

importMapping functionality lacking core functionality, such as rename ability.

Open derrick3192 opened this issue 8 years ago • 3 comments

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).

derrick3192 avatar Jun 20 '17 02:06 derrick3192

Was a solution found for this?

StephenOTT avatar Jun 26 '19 22:06 StephenOTT

+1

AlexandreCassagne avatar Jun 06 '22 12:06 AlexandreCassagne

xdx505 avatar Sep 01 '22 06:09 xdx505