Skip to content

Commit 2584c9d

Browse files
authored
add fromString() method to enums as required by JAX RS spec (#7494)
1 parent 896504d commit 2584c9d

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

modules/openapi-generator/src/main/resources/JavaJaxRS/spec/enumClass.mustache

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@
2121
return String.valueOf(value);
2222
}
2323

24+
/**
25+
* Convert a String into {{dataType}}, as specified in the
26+
* <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>
27+
*/
28+
public static {{datatypeWithEnum}} fromString(String s) {
29+
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
30+
// using Objects.toString() to be safe if value type non-object type
31+
// because types like 'int' etc. will be auto-boxed
32+
if (java.util.Objects.toString(b.value).equals(s)) {
33+
return b;
34+
}
35+
}
36+
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected string value '" + s + "'");{{/isNullable}}
37+
}
38+
2439
@JsonCreator
2540
public static {{datatypeWithEnum}} fromValue({{dataType}} value) {
2641
for ({{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {

modules/openapi-generator/src/main/resources/JavaJaxRS/spec/enumOuterClass.mustache

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ import com.fasterxml.jackson.annotation.JsonValue;
2525
this.value = value;
2626
}
2727

28+
/**
29+
* Convert a String into {{dataType}}, as specified in the
30+
* <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>
31+
*/
32+
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromString(String s) {
33+
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
34+
// using Objects.toString() to be safe if value type non-object type
35+
// because types like 'int' etc. will be auto-boxed
36+
if (java.util.Objects.toString(b.value).equals(s)) {
37+
return b;
38+
}
39+
}
40+
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected string value '" + s + "'");{{/isNullable}}
41+
}
42+
2843
@Override
2944
@JsonValue
3045
public String toString() {

0 commit comments

Comments
 (0)