forked from loafle/openapi-generator-original
add fromString() method to enums as required by JAX RS spec (#7494)
This commit is contained in:
parent
896504de51
commit
2584c9d99d
@ -21,6 +21,21 @@
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a String into {{dataType}}, as specified in the
|
||||
* <a href="https://download.oracle.com/otndocs/jcp/jaxrs-2_0-fr-eval-spec/index.html">See JAX RS 2.0 Specification, section 3.2, p. 12</a>
|
||||
*/
|
||||
public static {{datatypeWithEnum}} fromString(String s) {
|
||||
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
|
||||
// using Objects.toString() to be safe if value type non-object type
|
||||
// because types like 'int' etc. will be auto-boxed
|
||||
if (java.util.Objects.toString(b.value).equals(s)) {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected string value '" + s + "'");{{/isNullable}}
|
||||
}
|
||||
|
||||
@JsonCreator
|
||||
public static {{datatypeWithEnum}} fromValue({{dataType}} value) {
|
||||
for ({{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
|
||||
|
@ -25,6 +25,21 @@ import com.fasterxml.jackson.annotation.JsonValue;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a String into {{dataType}}, as specified in the
|
||||
* <a href="https://download.oracle.com/otndocs/jcp/jaxrs-2_0-fr-eval-spec/index.html">See JAX RS 2.0 Specification, section 3.2, p. 12</a>
|
||||
*/
|
||||
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromString(String s) {
|
||||
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
|
||||
// using Objects.toString() to be safe if value type non-object type
|
||||
// because types like 'int' etc. will be auto-boxed
|
||||
if (java.util.Objects.toString(b.value).equals(s)) {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected string value '" + s + "'");{{/isNullable}}
|
||||
}
|
||||
|
||||
@Override
|
||||
@JsonValue
|
||||
public String toString() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user