Swagger-codegen: [JAVA] Bug generating top level enum definitions (doesn't compile)

Created on 12 Jul 2016  路  25Comments  路  Source: swagger-api/swagger-codegen

Description

If I create an enum definition for using it in many objects, I get this file generated with compiling errors.

Swagger-codegen version

I'm using master

Swagger declaration file content or url
swagger: '2.0'

info:
  version: "1.0.0"
  title: Title was not specified
host: 'localhost:9090'
basePath: /base
schemes:
  - http
paths:
  /test:
    get:
      responses:
        '200':
          description: Success
          schema:
            $ref: '#/definitions/FooObject'
      operationId: getFooObject
definitions:
  FooObject:
    properties:
      myEnum:
        $ref: '#/definitions/BarEnum'
  BarEnum:
    type: string
    enum:
      - AENUM
      - BENUM
Command line used for generation

java -jar swagger-codegen-cli.jar generate -i test.yml -l spring -o swagger-output/

Output

I get this exact output

package io.swagger.model;

import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonValue;

public enum BarEnum {
    {values=[AENUM, BENUM], enumVars=[{name=AENUM, value="AENUM"}, {name=BENUM, value="BENUM"}]},
}
Enum Bug Java

Most helpful comment

3511 (fixed models with enum properties for spring) has been merged into master.

Please pull the latest master to give it a try.

All 25 comments

I'm seeing this as well. Shouldn't this work? I am using language=jaxrs and library=jersey2

Can you try library=okhttp-gson instead to see if that works for you?

(we've not applied enum enhancement to all Java-related generator yet)

This doesn't work:
java -jar swagger-codegen-cli.jar generate -i test.yml -l spring --library okhttp-gson -o swagger-output/

unknown library: okhttp-gson

@diega sorry I was not clear. okhttp-gson is for Java API client so please use -l java instead.

@wing328 no problem!
Yes, it seems like running java -jar swagger-codegen-cli.jar generate -i test.yml -l java --library okhttp-gson -o swagger-output/ generates the enum properly, but it does java -jar swagger-codegen-cli.jar generate -i test.yml -l java -o swagger-output/ as well. Indeed, I tried setting --library for all the options in the documentation (jersey2, feign, okhttp-gson, retrofit and retrofit2) and all generates the enum just fine.
It looks like something related to the spring language (and jaxrs, as pointed by @jeff9finger)

@diega thanks for performing more test to confirm. If you've cycle, we would welcome contribution to apply the same fix in Java API client to Java server stub generator.

@wing328 sure, is there some fix already applied to _client_ generation you think could be applied to the _server_ generation?

@diega are you using the latest master to generate the spring server stub?

As shown in https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/JavaSpring/model.mustache, the model generated in spring has support for enum already.

@wing328 yes I am.
Has you been able to reproduce it using the _yml_ definition I attached in my original report?

@diega I will give it a try later today (busy with other things at the moment...)

@wing328 thanks! is there any chance for it to be included into the v2.2.0 milestone?

cc @cbornet

I'm affected by this critical issue as well. Any news regarding including the fix in v2.2.0? Thanks!

I could repeat the issue using the spec provided by @diega

I'll try to fix it but not sure if we can merge the fix into 2.2.0 release, which should be due shortly.

I am going to give this a try. It looks like changes in the mustache templates.

@wing328 Are you working on this? Don't want to duplicate effort.

@jebentier I'm busy with other works. If you've time, please give it a try. Thanks.

As 2.2.0 will be out soon, we'll not include the fix, if ready, in the 2.2.0 release.

Just in case somebody needs some kind of _workaround_, running this on the root of your project cleanups the enums
$ find . -name *.java -print0 | xargs -0 sed -i '' 's/.*\[\(.*\)\],.*/ \1/'

@jeff9finger did you have a chance to work on the fix?

UPDATE: I've submitted to https://github.com/swagger-api/swagger-codegen/pull/3506 to fix the issue for JAX-RS (and also another issue with PHP enum handling).

I'll work on the fix for spring tomorrow.

Thanks. I have not been able to get to this.

3511 (fixed models with enum properties for spring) has been merged into master.

Please pull the latest master to give it a try.

I can confirm this is working now. Thanks @wing328!

Yes thanks @wing328 !!!!

Was this page helpful?
0 / 5 - 0 ratings