forked from quarkiverse/quarkus-openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft to support unexpected enum values
resolves quarkiverse#692
- Loading branch information
1 parent
d0f8224
commit d2f95fd
Showing
14 changed files
with
299 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...c/main/resources/templates/libraries/microprofile/additionalEnumTypeUnexpectedMember.qute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Special value if the API response contains some new value not declared in this enum. | ||
* You should react accordingly. | ||
*/ | ||
UNEXPECTED({#if e.isContainer}{e.items.dataType}{#else}{e.dataType}{/if}.valueOf("unexpected")){#if e.allowableValues},{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>quarkus-openapi-generator-integration-tests</artifactId> | ||
<groupId>io.quarkiverse.openapi.generator</groupId> | ||
<version>3.0.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>quarkus-openapi-generator-it-enum-unexpected</artifactId> | ||
<name>Quarkus - Openapi Generator - Integration Tests - Enum Unexpected</name> | ||
<description>Example project for OpenAPI with enum unexpected value</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkiverse.openapi.generator</groupId> | ||
<artifactId>quarkus-openapi-generator</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-junit5</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.tomakehurst</groupId> | ||
<artifactId>wiremock-jre8</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-maven-plugin</artifactId> | ||
<extensions>true</extensions> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>build</goal> | ||
<goal>generate-code</goal> | ||
<goal>generate-code-tests</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<profiles> | ||
<profile> | ||
<id>native-image</id> | ||
<activation> | ||
<property> | ||
<name>native</name> | ||
</property> | ||
</activation> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<skipTests>${native.surefire.skip}</skipTests> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-failsafe-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>integration-test</goal> | ||
<goal>verify</goal> | ||
</goals> | ||
<configuration> | ||
<systemPropertyVariables> | ||
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path> | ||
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> | ||
<maven.home>${maven.home}</maven.home> | ||
</systemPropertyVariables> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<properties> | ||
<quarkus.package.type>native</quarkus.package.type> | ||
</properties> | ||
</profile> | ||
</profiles> | ||
|
||
</project> |
28 changes: 28 additions & 0 deletions
28
client/integration-tests/enum-unexpected/src/main/openapi/with-enum-unexpected.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
openapi: 3.0.3 | ||
info: | ||
title: echo | ||
version: '1.0.0' | ||
description: "" | ||
paths: | ||
/echo: | ||
post: | ||
summary: Echo | ||
operationId: echo | ||
responses: | ||
"200": | ||
description: OK | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Echo' | ||
components: | ||
schemas: | ||
Echo: | ||
type: object | ||
required: | ||
- msgType | ||
properties: | ||
msgType: | ||
type: string | ||
enum: | ||
- 'text' |
28 changes: 28 additions & 0 deletions
28
client/integration-tests/enum-unexpected/src/main/openapi/without-enum-unexpected.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
openapi: 3.0.3 | ||
info: | ||
title: echo | ||
version: '1.0.0' | ||
description: "" | ||
paths: | ||
/echo: | ||
post: | ||
summary: Echo | ||
operationId: echo | ||
responses: | ||
"200": | ||
description: OK | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Echo' | ||
components: | ||
schemas: | ||
Echo: | ||
type: object | ||
required: | ||
- msgType | ||
properties: | ||
msgType: | ||
type: string | ||
enum: | ||
- 'text' |
4 changes: 4 additions & 0 deletions
4
client/integration-tests/enum-unexpected/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
quarkus.openapi-generator.codegen.spec.with_enum_unexpected_yaml.additional-enum-type-unexpected-member=true | ||
|
||
|
||
quarkus.openapi-generator.codegen.spec.without_enum_unexpected_yaml.additional-enum-type-unexpected-member=false |
113 changes: 113 additions & 0 deletions
113
...enum-unexpected/src/test/java/io/quarkiverse/openapi/generator/it/EnumUnexpectedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package io.quarkiverse.openapi.generator.it; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.post; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.assertj.core.api.AssertionsForClassTypes; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.hamcrest.MatcherAssert; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.exc.ValueInstantiationException; | ||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import com.github.tomakehurst.wiremock.core.WireMockConfiguration; | ||
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer; | ||
|
||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
@QuarkusTestResource(EnumUnexpectedTest.EchoMockServer.class) | ||
class EnumUnexpectedTest { | ||
|
||
@RestClient | ||
@Inject | ||
org.openapi.quarkus.with_enum_unexpected_yaml.api.DefaultApi api; | ||
|
||
@Test | ||
void apiIsBeingGenerated() { | ||
|
||
org.openapi.quarkus.with_enum_unexpected_yaml.model.Echo echo = api.echo(); | ||
|
||
Assertions.assertThat(echo.getMsgType()) | ||
.isEqualTo(org.openapi.quarkus.with_enum_unexpected_yaml.model.Echo.MsgTypeEnum.UNEXPECTED); | ||
} | ||
|
||
@Inject | ||
ObjectMapper objectMapper; | ||
|
||
@Test | ||
void when_additional_enum_type_unexpected_member_is_true_should_have_extra_member() { | ||
AssertionsForClassTypes.assertThat(org.openapi.quarkus.with_enum_unexpected_yaml.model.Echo.MsgTypeEnum.class) | ||
.hasOnlyPublicFields("TEXT", "UNEXPECTED"); | ||
} | ||
|
||
@Test | ||
void when_additional_enum_type_unexpected_is_false_should_not_have_extra_member() { | ||
AssertionsForClassTypes.assertThat(org.openapi.quarkus.without_enum_unexpected_yaml.model.Echo.MsgTypeEnum.class) | ||
.hasOnlyPublicFields("TEXT"); | ||
} | ||
|
||
@Test | ||
void when_additional_enum_type_unexpected_member_is_true_should_parse_unknown_values_to_UNEXPECTED() | ||
throws JsonProcessingException { | ||
org.openapi.quarkus.with_enum_unexpected_yaml.model.Echo actualEcho = objectMapper.readValue("{ \"msgType\": \"NOPE\"}", | ||
org.openapi.quarkus.with_enum_unexpected_yaml.model.Echo.class); | ||
MatcherAssert.assertThat(actualEcho.getMsgType(), | ||
is(org.openapi.quarkus.with_enum_unexpected_yaml.model.Echo.MsgTypeEnum.UNEXPECTED)); | ||
} | ||
|
||
@Test | ||
void when_additional_enum_type_unexpected_member_is_false_should_fail_parsing_unknown_values() | ||
throws JsonProcessingException { | ||
assertThatThrownBy(() -> { | ||
objectMapper.readValue("{ \"msgType\": \"NOPE\"}", | ||
org.openapi.quarkus.without_enum_unexpected_yaml.model.Echo.class); | ||
}).isInstanceOf(ValueInstantiationException.class); | ||
} | ||
|
||
public static class EchoMockServer implements QuarkusTestResourceLifecycleManager { | ||
|
||
private WireMockServer wireMockServer; | ||
|
||
@Override | ||
public Map<String, String> start() { | ||
configureWiremockServer(); | ||
return Collections.singletonMap("org.openapi.quarkus.with_enum_unexpected_yaml.api.DefaultApi/mp-rest/url", | ||
wireMockServer.baseUrl()); | ||
} | ||
|
||
private void configureWiremockServer() { | ||
var wireMockConfiguration = WireMockConfiguration.wireMockConfig() | ||
.extensions(new ResponseTemplateTransformer(false)).dynamicPort(); | ||
wireMockServer = new WireMockServer(wireMockConfiguration); | ||
wireMockServer.start(); | ||
|
||
wireMockServer.stubFor(post(urlEqualTo("/echo")) | ||
.willReturn(aResponse() | ||
.withStatus(200) | ||
.withHeader("Content-Type", "application/json") | ||
.withBody("{ \"msgType\": \"NOPE\"}") | ||
.withTransformers("response-template"))); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
if (wireMockServer != null) { | ||
wireMockServer.stop(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters