Skip to content

Commit

Permalink
[Java][jaxrs-spec] enumUnknownDefaultCase true now returns correctly …
Browse files Browse the repository at this point in the history
…in fromValue (#18910)

* enumUnknownDefaultCase true added for Jaxrs-spec

* Updated tests

---------

Co-authored-by: Dean <[email protected]>
  • Loading branch information
wing328 and DeaneOC authored Jun 12, 2024
1 parent ef76549 commit fc0105e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
return b;
}
}
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}{{#enumUnknownDefaultCase}}{{#allowableValues}}{{#enumVars}}{{#-last}}return {{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}{{/enumUnknownDefaultCase}}{{^enumUnknownDefaultCase}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/enumUnknownDefaultCase}}{{/isNullable}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ import com.fasterxml.jackson.annotation.JsonValue;
return b;
}
}
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}{{#enumUnknownDefaultCase}}{{#allowableValues}}{{#enumVars}}{{#-last}}return {{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}{{/enumUnknownDefaultCase}}{{^enumUnknownDefaultCase}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/enumUnknownDefaultCase}}{{/isNullable}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1033,4 +1033,51 @@ public void generateSpecNonInterfaceWithMicroprofileOpenApiAnnotations() throws
" info = @org.eclipse.microprofile.openapi.annotations.info.Info(\n" +
" title = \"user\", version=\"1.0.0\", description=\"Operations about user\",");
}

@Test
public void testEnumUnknownDefaultCaseDeserializationTrue_issue13444() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/bugs/issue_13444.yaml", null, new ParseOptions()).getOpenAPI();

codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, "true");

ClientOptInput input = new ClientOptInput()
.openAPI(openAPI)
.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));

JavaFileAssert.assertThat(files.get("Color.java"))
.assertMethod("fromValue").bodyContainsLines("return UNKNOWN_DEFAULT_OPEN_API");

}

@Test
public void testEnumUnknownDefaultCaseDeserializationNotSet_issue13444() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/bugs/issue_13444.yaml", null, new ParseOptions()).getOpenAPI();

codegen.setOutputDir(output.getAbsolutePath());

ClientOptInput input = new ClientOptInput()
.openAPI(openAPI)
.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));

JavaFileAssert.assertThat(files.get("Color.java"))
.assertMethod("fromValue").bodyContainsLines("throw new IllegalArgumentException(\"Unexpected value '\" + value + \"'\");");

}
}
17 changes: 17 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_13444.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
openapi: "3.0.0"
info:
version: 2.0.0
title: test
paths:
/pets:
get:
summary: List all pets
operationId: listPets
responses:
'200':
description: OK
components:
schemas:
Color:
type: string
enum: [RED, BLUE, GREEN]

0 comments on commit fc0105e

Please sign in to comment.