swagger-api / swagger-api/swagger-codegen

[Java] --model-name-prefix incompatible with oneOf

Open
#10,442 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

--model-name-prefix in combination with oneOf produces Java code that does not compile.

Swagger-codegen version

3.0.21

Swagger declaration file content or url
openapi: 3.0.1                                                                                                                                                                 
info:                                                                                                                                                                          
  version: "1.0.0"                                                                                                                                                             
  title: Swagger Petstore                                                                                                                                                      
                                                                                                                                                                               
paths:                                                                                                                                                                         
  /pet:                                                                                                                                                                        
    post:                                                                                                                                                                      
      responses:                                                                                                                                                               
        '202'                                                                                                                                                                  
      requestBody:                                                                                                                                                             
        $ref: '#/components/requestBodies/Pet'                                                                                                                                 
                                                                                                                                                                               
components:                                                                                                                                                                    
  requestBodies:                                                                                                                                                               
    Pet:                                                                                                                                                                       
      content:                                                                                                                                                                 
        application/json:                                                                                                                                                      
          schema:                                                                                                                                                              
            $ref: '#/components/schemas/Pet'                                                                                                                                   
  schemas:                                                                                                                                                                     
    Pet:                                                                                                                                                                       
      type: object                                                                                                                                                             
      properties:                                                                                                                                                              
        baz:                                                                                                                                                                   
          oneOf:                                                                                                                                                               
            - $ref: "#/components/schemas/A"                                                                                                                                   
            - $ref: "#/components/schemas/B"                                                                                                                                   
          discriminator: discrim                                                                                                                                               
    A:                                                                                                                                                                         
      type: object                                                                                                                                                             
      properties:                                                                                                                                                              
        discrim:                                                                                                                                                               
          type: string                                                                                                                                                         
    B:                                                                                                                                                                         
      type: object                                                                                                                                                             
      properties:                                                                                                                                                              
        discrim:
          type: string
Command line used for generation

java -jar swagger-codegen-cli.jar generate -i petstore.yaml -l java --model-name-prefix=Foo

Steps to reproduce

Inspect the generated FooPet source model. This won't compile; it tries to import a non-existent model FoObject.

head -n24 src/main/java/io/swagger/client/model/FooPet.java | tail -n12
package io.swagger.client.model;

import java.util.Objects;
import java.util.Arrays;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.swagger.client.model.FooObject;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.IOException;
Suggest a fix/enhancement

Fix imports: should not import FooObject. It could either import nothing instead, or perhaps instead import FooOneOfPetFooBaz (it's in the same package, but the rest of the codegen seems to import these anyways).

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

Reproduce the issue with the provided OpenAPI YAML and Java generation command, then inspect the generated src/main/java/io/swagger/client/model/FooPet.java import section. Trace the Java model/import generation path responsible for FooObject; done means the generated code compiles without importing a nonexistent model.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.