java - jsonschema2pojo generating enum without any key value pairs -
i using jsonschema2pojo generate pojo json schema.however, not seem working when using named enums.instead of key value pairs in pojo, adds __empty__
. there problem way have specified enums or issue jsonschema2pojo?
json schema
{ "type": "object", "$schema": "http://json-schema.org/draft-04/schema", "description": "status identifier", "properties": { "status": { "type": "string", "enum": [ {"active" : 0}, {"inactive" : 1} ], "description": "defines whether schedule active" } } }
generated java pojo
import java.util.hashmap; import java.util.map; import javax.annotation.generated; import com.fasterxml.jackson.annotation.jsonanygetter; import com.fasterxml.jackson.annotation.jsonanysetter; import com.fasterxml.jackson.annotation.jsoncreator; import com.fasterxml.jackson.annotation.jsonignore; import com.fasterxml.jackson.annotation.jsoninclude; import com.fasterxml.jackson.annotation.jsonproperty; import com.fasterxml.jackson.annotation.jsonpropertyorder; import com.fasterxml.jackson.annotation.jsonvalue; import org.apache.commons.lang.builder.equalsbuilder; import org.apache.commons.lang.builder.hashcodebuilder; import org.apache.commons.lang.builder.tostringbuilder; /** * status identifier * */ @jsoninclude(jsoninclude.include.non_null) @generated("org.jsonschema2pojo") @jsonpropertyorder({ "status" } ) public class testenum { /** * defines whether schedule active * */ @jsonproperty("status") private testenum.status status; @jsonignore private map<string, object> additionalproperties=new hashmap<string, object>(); /** * defines whether schedule active * * @return * status */ @jsonproperty("status") public testenum.status getstatus() { return status; } /** * defines whether schedule active * * @param status * status */ @jsonproperty("status") public void setstatus(testenum.status status) { this.status=status; } @override public string tostring() { return tostringbuilder.reflectiontostring(this); } @jsonanygetter public map<string, object> getadditionalproperties() { return this.additionalproperties; } @jsonanysetter public void setadditionalproperty(string name, object value) { this.additionalproperties.put(name, value); } @override public int hashcode() { return new hashcodebuilder().append(status).append(additionalproperties).tohashcode(); } @override public boolean equals(object other) { if (other==this) { return true; } if ((other instanceof testenum)==false) { return false; } testenum rhs=((testenum) other); return new equalsbuilder().append(status, rhs.status).append(additionalproperties, rhs.additionalproperties).isequals(); } @generated("org.jsonschema2pojo") public static enum status { __empty__("", ""); private final string value; private static map<string, testenum.status> constants=new hashmap<string, testenum.status>(); static { (testenum.status c: values()) { constants.put(c.value, c); } } private status(string value) { this.value=value; } @jsonvalue @override public string tostring() { return this.value; } @jsoncreator public static testenum.status fromvalue(string value) { testenum.status constant=constants.get(value); if (constant==null) { throw new illegalargumentexception(value); } else { return constant; } } } }
note - works fine if remove enum names , specify values below
{ "type": "object", "$schema": "http://json-schema.org/draft-04/schema", "description": "status identifier", "properties": { "status": { "type": "string", "enum": [ "active" , "inactive" ], "description": "defines whether schedule active" } } }
[edit]
tl;dr: defining key-value pairs instead of json array, schema not in fitting json-schema.org's definition.
[/edit]
in java, an enum best used either creating singleton or enforcing value fixed number of constants. fixed set of constants value list.
that being said, since (first) json schema's definition of status property it's string , can value of set of objects, opposed value of set of values. in other words, according documentation jsonschema2pojo on defining enum, there no example of set of objects, set of values. it's expecting list of values , nothing more.
your example @ end of question, "it works fine if remove enum names , specify values below," successful result because defining property build out enum according jsonschema2pojo expecting.
my recommendation: use last example , either build separate map<string, integer> pair "active" , "inactive" values numeric representations (if expect have larger list), or set couple constants map numeric value (e.g.- private static int active = 1;
), needs may be.
Comments
Post a Comment