Nested Definition of Primitive Types Generate Empty Classes

Open
#7,008 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Reproduce the issue with the supplied petstore.yaml and the swagger-codegen CLI command, then compare Pet.java and StringType.java with the expected Java String property. Start by reviewing the prior fix for #5382 and its primitive-alias detection. Done means nested primitive aliases such as stringType generate the standard Java primitive wrapper rather than an empty class.

Written by the indexing model from the issue text.

Description

Description

Nested definitions of a primitive type (e.g. string, number, integer) end up generating empty classes (Java).

Essentially, this is the same issue as #5382, except that these definitions are nested more than 1 layer deep.

Swagger-codegen version

2.3.0-SNAPSHOT, master

Swagger declaration file content or url
swagger: "2.0"
info:
  version: 1.0.0
  title: Swagger Petstore
paths:
  /pet:
    get:
      responses:
        "200":
          description: Expected response to a valid request
          schema:
            $ref: '#/definitions/Pet'
definitions:
  Pet:
    properties:
      name:
        $ref: "#/definitions/stringType"
  stringType:
    $ref: "#/definitions/string"
  string:
    type: string

Generated code: model.zip

  • Pet.java's name property references a StringType class
  • StringType.java is an empty class

Expected code:

  • Pet.java's name property should be a standard Java String
Command line used for generation
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
  -i petstore.yaml \
  -l spring \
  -o zzzzz

Also generated via http://editor.swagger.io , with the same results.

Steps to reproduce

Any nesting of primitive types that is more than 1 level will reproduce this. Also occurs for other primitive types such as number, integer, etc.

Related issues/PRs

#5382

Suggest a fix/enhancement

Not familiar with the codebase, but my guess is that the prior fix for #5382 @bbdouglas (which added detection for whether a JSON node is an alias for another type) may need to be updated to recurse into itself to check for deeper nesting of these aliases.

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 Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.