Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enums are not generated in Java/Android Codegen #315

Closed
maneeshsahu opened this issue Oct 26, 2014 · 7 comments
Closed

enums are not generated in Java/Android Codegen #315

maneeshsahu opened this issue Oct 26, 2014 · 7 comments

Comments

@maneeshsahu
Copy link

This property was not codgen-ed:

   "order": {
      "type": "string",
      "enum": [
        "asc",
        "desc"
      ]
    }

as
private OrderEnum order = null;

But the Class - OrderEnum was not generated.

@ghost
Copy link

ghost commented Jul 1, 2015

faced same issue today. in definitions section i have:

"A": {
    "type": "string",
    "enum": [
        "VALUE_1",
        "VALUE_2"
    ]
}

result of generation is empty A class. if i wrap this in definition named B and set A as property - class B with one field (A as enum) generated correctly but IMO looks a little bit ugly.
maybe type "enum" may be introduced?..

@xhh
Copy link
Contributor

xhh commented Jul 2, 2015

The enum class is generated correctly right now.
Please have a look at here:

public class Pet  {

  private Long id = null;
  private Category category = null;
  private String name = null;
  private List<String> photoUrls = new ArrayList<String>() ;
  private List<Tag> tags = new ArrayList<Tag>() ;
  public enum StatusEnum {
     available,  pending,  sold, 
  };
  private StatusEnum status = null;

That is, the enum class (StatusEnum above) is generated inline within the model class containing the property (status).
Below is some code verifying that it works:

PetApi api = new PetApi();

Pet pet = new Pet();
pet.setId(new Long(9527));
pet.setName("Kitty");
pet.setStatus(Pet.StatusEnum.pending);
api.addPet(pet);

Pet pet = api.getPetById(new Long(9527));
System.out.println(pet);
System.out.println(pet.getStatus());
System.out.println(pet.getStatus().getClass());

/* OUTPUT
class Pet {
  id: 9527
  category: null
  name: Kitty
  photoUrls: []
  tags: []
  status: pending
}

pending
class io.swagger.client.model.Pet$StatusEnum
*/

List<Pet> pets = api.findPetsByStatus(Arrays.asList("pending"));
System.out.println(pets);

/* OUTPUT
[class Pet {
  id: 9527
  category: null
  name: Kitty
  photoUrls: []
  tags: []
  status: pending
}
]
*/

@sunrisejjj regarding "result of generation is empty A class", are you expecting to generate separate enum class? If you are, I would say that the current nesting enum class would be better, to distinguish different enum classes with the same property name, for example there's also a status enum in the Order model (find it here) with different values:

  public enum StatusEnum {
     placed,  approved,  delivered, 
  };

@ghost
Copy link

ghost commented Jul 2, 2015

@xhh currently i have two different options:

  • declare this enum wrapped in another entity, like this (but not looks really pretty for me)
"workflowStatus": {
    "description": "Wrapper for Workflow Status enum",
    "properties": {
        "status": {
            "type": "string",
            "enum": [
                "Status_1",
                "Status_2",
                "Status_3",
                "Resolved"
            ]
        }
    }
}
  • declare this enum as inline enum (like you described) in different entities to 're-use' it
    but it is not really nice because e.g. if i use this enum in ten entities and want to add new parameter for this enum - i need to find all ten usages and add additional parameter for all of them
    instead of going to one (and only :) ) enum declaration and add parameter to it

that's the reason why i want to generate 'stand-alone' enum classes.

@xhh
Copy link
Contributor

xhh commented Jul 2, 2015

@sunrisejjj I agree with you that in the case you described it would be better to generate standalone enum class. However, the generator is not perfect/smart enough for now to detect the sharing of same enum classes, or to distinguish cases of whether to generate inline or standalone enum classes. And for now the inline approach would be the more "practical" one, before we making the generator perfect enough for enum (if possible/necessary).

Regarding changing enum values, I think that would need changing those enum values for all models (definitions) in the spec file, right? After that rerunning the codegen will just generate the new code.

@ghost
Copy link

ghost commented Jul 2, 2015

@xhh agree, yep, i understand that after change in contract new values will be generated, but it will be better if i need to change my enum just in one place instead of many (i think that it is valid case for large web-services)

by the way, current approach is acceptable! it's just about improvements. :)

@who
Copy link
Contributor

who commented Aug 5, 2015

@xhh Currently, because enums are not generated as stand-alone classes, the java output of swagger-codegen will not compile when using enums inside subTypes. I have filed a bug here: #1050

@wing328
Copy link
Contributor

wing328 commented Nov 1, 2015

Should be resolved. Please reopen if needed.

@wing328 wing328 closed this as completed Nov 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants