When using and array of an enum type the generated model in java doesn't compile.
v2.2.0
swagger: '2.0'
info:
title: Enum Test
description: Test enum inside array
version: "1.0.0"
host: localhost:8080
schemes:
- http
- https
consumes:
- "application/json"
produces:
- "application/json"
paths:
/boxes:
get:
responses:
200:
schema:
type: array
items:
$ref: "#/definitions/box"
definitions:
box:
properties:
id:
type: string
arrayprop:
type: array
items:
type: string
enum:
- A
- B
- C
public class BoxModel {
//...
public enum java.util.List<ArraypropEnum> {
private String value;
java.util.List<ArraypropEnum>(String value) {
this.value = value;
}
@Override
@com.fasterxml.jackson.annotation.JsonValue
public String toString() {
return value;
}
}
public enum ArraypropEnum {
A("A"),
B("B"),
C("C");
private String value;
ArraypropEnum(String value) {
this.value = value;
}
@Override
@com.fasterxml.jackson.annotation.JsonValue
public String toString() {
return value;
}
}
protected java.util.List<ArraypropEnum> arrayprop = new java.util.ArrayList<ArraypropEnum>();
//...
}
The list is not an enum. This class must not exists:
public enum java.util.List<ArraypropEnum> {
// ...
}
In DefaultCodegen.updatePropertyForArray
/**
* Update property for array(list) container
* @param property Codegen property
* @param innerProperty Codegen inner property of map or list
*/
protected void updatePropertyForArray(CodegenProperty property, CodegenProperty innerProperty) {
if (innerProperty == null) {
LOGGER.warn("skipping invalid array property " + Json.pretty(property));
} else {
if (!languageSpecificPrimitives.contains(innerProperty.baseType)) {
property.complexType = innerProperty.baseType;
} else {
property.isPrimitiveType = true;
}
property.items = innerProperty;
// inner item is Enum
if (isPropertyInnerMostEnum(property)) {
property.isEnum = true;
// update datatypeWithEnum and default value for array
// e.g. List<string> => List<StatusEnum>
updateDataTypeWithEnumForArray(property);
}
}
}
I eliminated the property.isEnum = true; inside the condicion if (isPropertyInnerMostEnum(property))
The modified method must be:
/**
* Update property for array(list) container
* @param property Codegen property
* @param innerProperty Codegen inner property of map or list
*/
protected void updatePropertyForArray(CodegenProperty property, CodegenProperty innerProperty) {
if (innerProperty == null) {
LOGGER.warn("skipping invalid array property " + Json.pretty(property));
} else {
if (!languageSpecificPrimitives.contains(innerProperty.baseType)) {
property.complexType = innerProperty.baseType;
} else {
property.isPrimitiveType = true;
}
property.items = innerProperty;
// inner item is Enum
if (isPropertyInnerMostEnum(property)) {
// update datatypeWithEnum and default value for array
// e.g. List<string> => List<StatusEnum>
updateDataTypeWithEnumForArray(property);
}
}
}
I think that this line set the list as an enum (thing that we do not want).
With this change the generated class doesn't have the extra "enum list" that it's wrong.
I'm not sure if this is the right solution and if this affects other cases.
Thanks!
Regards,
Adrian
I had the same problem. This change in pojo.mustache fixes the problem:
{{#vars}}{{#isEnum}}{{#isNotContainer}}
{{>enumClass}}{{/isNotContainer}}{{/isEnum}}{{#items.isEnum}}{{#items}}
I agree with @fiore-adrian -- and thank your for reporting this including a fix.
I have the same problem in a PHP client so this is not isolated to the Java server but probably affects all generated clients and servers.
The fix from @fiore-adrian looks good to me (and does the right thing in my PHP client as well).
@wing328, can you see any problems in this? Or should we have a better fix for this (checking the enums inside the array)?
I suggest we apply #3451 (so we don't generate non-working code) until we maybe find a better solution.
@fiore-adrian thanks for reporting the issue
@arnested thanks for reviewing the fix.
@frasch1712 thanks for sharing the fix.
The reason why I set isEnum to true can be found in https://github.com/swagger-api/swagger-codegen/issues/3323 so I prefer the fix using {{^isContainer}} similar to what @frasch1712 has suggested (we've applied similar fix to Ruby and other clients)
UPDATE: I'm working on a fix for PHP and JAX-RX.
I've filed https://github.com/swagger-api/swagger-codegen/pull/3506 to fix the issue for JAX-RS, PHP.
Please do a test if you've time:
git checkout -b wing328-fix_array_enum master
git pull https://github.com/wing328/swagger-codegen.git fix_array_enum
PR merged into master. Please pull the latest master to give it a try.
Most helpful comment
UPDATE: I'm working on a fix for PHP and JAX-RX.