Swagger-Codegen is creating wrong import for domain models

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

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
java
Domain
tooling

Research direction

Start in io.swagger.codegen.languages.DefaultCodegenConfig.java, specifically getSchemaType, and compare its handling of #/definitions/ references with the generated Pagination.java import. Reproduce the supplied Pagination and PageLink models, then verify that the generated import uses PageLink rather than DefinitionsPageLink and that the Java output compiles.

Written by the indexing model from the issue text.

Description

Description:
Swagger codegen is creating wrong import statements in java model class for domain models which results in compile errors.

Swagger-codegen version
3.0.0-rc1

Created following domain models

 ##Pagination Data models
  Pagination:
    type: object
    description: "Pagination Metada which is used to communicate pagination related fields between consumer and API implementation" 
    properties:
      correlationid:
        type: string
        maxLength: 50
        description: "Used to identify the record cache if the pagination logic uses a server side cache." 
        example: "BLUSRLIST_101"
      pageSize:
        type: integer
        default: 10
        description: "Number of records to be returned in a single page. API will use default value if this is not supplied"
        example: 10
      totalRecords:
        type: integer
        readOnly: true
        description: "Total number of records. This can be used by the consumer to decide the page size."
        example: 100
      pageNumber:
        type: integer
        readOnly: true
        description: "Page number of the current records."
        example: 1
      lastPageNumber:
        type: integer
        readOnly: true
        description: "Page number of the last page based on total records."          
        example: 10  
      startIndex:  
        type: string
        readOnly: true
        description: "Start index/ID of records in the current page. This is the start cursor of the record set representing a page.This can be continutation token to fetch the previous page."
        example: "u1001"
      endIndex:  
        type: string
        readOnly: true
        description: "End index/ID of records in the current page.This is the end cursor of the record set representing a page. This can be continutation token to fetch the next page."
        example: "u1010"
      links:
        type: array
        description: "Page links for previous, next, first and last pages"
        items: 
          $ref: "#/definitions/PageLink"
          
  PageLink:
    type: object
    description: "Represents first, last, previous and next page links in a pagination context."
    properties:
      next:
        type: string
        maxLength: 150
        description: "Represents the next page link"
      prev:
        type: string
        maxLength: 150
        description: "Represents the previous page link"
      first:
        type: string
        maxLength: 150
        description: "Represents the first page link"
      last:
        type: string
        maxLength: 150
        description: "Represents the last page link"

When we generate code in Pagination.java file it add DefinitionsPageLink this should be only PageLink.

package io.swagger.rest.model.v1;

import io.swagger.rest.model.v1.DefinitionsPageLink;
import io.swagger.annotations.ApiModel;
import java.util.ArrayList;
import java.util.List;

Suggest a fix/enhancement

Need to handle following condition in getSchemaType method of
io.swagger.codegen.languages.DefaultCodegenConfig.java of swagger-codegen-generators-1.0.0-rc1.jar

if(datatype.indexOf("#/definitions/") == 0) {
    	datatype = datatype.substring("#/definitions/".length());
                   return datatype;
  }
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

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.

More from swagger-api/swagger-codegen

All issues in swagger-api/swagger-codegen

Similar issues

More DevTools issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.