-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue: #219
- Loading branch information
Showing
15 changed files
with
571 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
69 changes: 69 additions & 0 deletions
69
example/src/main/resources/openapi-oneof-enum-discriminator.yml
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,69 @@ | ||
openapi: "3.0.0" | ||
info: { } | ||
|
||
paths: { } | ||
|
||
components: | ||
schemas: | ||
User: | ||
required: | ||
- id | ||
- username | ||
allOf: | ||
- $ref: '#/components/schemas/BaseUser' | ||
properties: | ||
id: | ||
type: string | ||
minLength: 5 | ||
username: | ||
type: string | ||
age: | ||
type: integer | ||
format: int32 | ||
minimum: 18 | ||
maximum: 199 | ||
writeOnly: true | ||
email: | ||
type: string | ||
nullable: true | ||
Admin: | ||
required: | ||
- id | ||
- adminname | ||
allOf: | ||
- $ref: '#/components/schemas/BaseUser' | ||
properties: | ||
id: | ||
type: string | ||
minLength: 5 | ||
adminname: | ||
type: string | ||
level: | ||
type: integer | ||
format: int64 | ||
minimum: 1 | ||
|
||
BaseUser: | ||
required: | ||
- type | ||
properties: | ||
type: | ||
type: string | ||
enum: | ||
- user | ||
- admin | ||
|
||
AdminOrUser: | ||
oneOf: | ||
- $ref: '#/components/schemas/Admin' | ||
- $ref: '#/components/schemas/User' | ||
discriminator: | ||
propertyName: type | ||
mapping: | ||
admin: '#/components/schemas/Admin' | ||
user: '#/components/schemas/User' | ||
|
||
|
||
|
||
|
||
|
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
78 changes: 78 additions & 0 deletions
78
...java/com/github/muehmar/gradle/openapi/oneof/DiscriminatorMappingDeserialisationTest.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,78 @@ | ||
package com.github.muehmar.gradle.openapi.oneof; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.muehmar.gradle.openapi.util.MapperFactory; | ||
import com.github.muehmar.openapi.util.Tristate; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class DiscriminatorMappingDeserialisationTest { | ||
private static final ObjectMapper MAPPER = MapperFactory.mapper(); | ||
|
||
@Test | ||
void fold_when_matchesAdmin_then_adminDtoReturned() throws JsonProcessingException { | ||
final AdminOrUserDiscriminatorMappingDto adminOrUserDto = | ||
MAPPER.readValue( | ||
"{\"id\":\"admin-id\",\"type\":\"adm\",\"adminname\":\"admin-name\",\"level\":5.5}", | ||
AdminOrUserDiscriminatorMappingDto.class); | ||
|
||
final Object obj = adminOrUserDto.foldOneOf(admin -> admin, user -> user); | ||
|
||
final AdminDto adminDto = | ||
AdminDto.builder() | ||
.setId("admin-id") | ||
.setType("adm") | ||
.setAdminname("admin-name") | ||
.andAllOptionals() | ||
.setLevel(5L) | ||
.build(); | ||
assertEquals(adminDto, obj); | ||
} | ||
|
||
@Test | ||
void fold_when_matchesUser_then_userDtoReturned() throws JsonProcessingException { | ||
final AdminOrUserDiscriminatorMappingDto adminOrUserDto = | ||
MAPPER.readValue( | ||
"{\"id\":\"user-id\",\"type\":\"usr\",\"username\":\"user-name\",\"age\":25,\"email\":null}", | ||
AdminOrUserDiscriminatorMappingDto.class); | ||
|
||
final Object obj = adminOrUserDto.foldOneOf(admin -> admin, user -> user); | ||
|
||
final UserDto userDto = | ||
UserDto.builder() | ||
.setId("user-id") | ||
.setType("usr") | ||
.setUsername("user-name") | ||
.andAllOptionals() | ||
.setAge(25) | ||
.setEmail(Tristate.ofNull()) | ||
.build(); | ||
assertEquals(userDto, obj); | ||
} | ||
|
||
@Test | ||
void fold_when_invalidTypeWithoutOnInvalid_then_exceptionThrown() throws JsonProcessingException { | ||
final AdminOrUserDiscriminatorMappingDto adminOrUserDto = | ||
MAPPER.readValue( | ||
"{\"id\":\"admin-id\",\"type\":\"Admin\",\"adminname\":\"admin-name\",\"level\":5.5}", | ||
AdminOrUserDiscriminatorMappingDto.class); | ||
|
||
assertThrows( | ||
IllegalStateException.class, () -> adminOrUserDto.foldOneOf(admin -> admin, user -> user)); | ||
} | ||
|
||
@Test | ||
void fold_when_invalidTypeWithOnInvalid_then_onInvalidReturned() throws JsonProcessingException { | ||
final AdminOrUserDiscriminatorMappingDto adminOrUserDto = | ||
MAPPER.readValue( | ||
"{\"id\":\"admin-id\",\"type\":\"Admin\",\"adminname\":\"admin-name\",\"level\":5.5}", | ||
AdminOrUserDiscriminatorMappingDto.class); | ||
|
||
final Object obj = adminOrUserDto.foldOneOf(admin -> admin, user -> user, () -> "invalid"); | ||
|
||
assertEquals("invalid", obj); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...t/java/com/github/muehmar/gradle/openapi/oneof/DiscriminatorMappingSerialisationTest.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,50 @@ | ||
package com.github.muehmar.gradle.openapi.oneof; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.muehmar.gradle.openapi.util.MapperFactory; | ||
import com.github.muehmar.openapi.util.Tristate; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class DiscriminatorMappingSerialisationTest { | ||
private static final ObjectMapper MAPPER = MapperFactory.mapper(); | ||
|
||
@Test | ||
void writeValueAsString_when_adminDto_then_correctJson() throws JsonProcessingException { | ||
final AdminDto adminDto = | ||
AdminDto.builder() | ||
.setId("admin-id") | ||
.setType("type") | ||
.setAdminname("admin-name") | ||
.andAllOptionals() | ||
.setLevel(5L) | ||
.build(); | ||
final AdminOrUserDiscriminatorMappingDto dto = | ||
AdminOrUserDiscriminatorMappingDto.builder().setAdminDto(adminDto).build(); | ||
|
||
assertEquals( | ||
"{\"adminname\":\"admin-name\",\"id\":\"admin-id\",\"level\":5,\"type\":\"adm\"}", | ||
MAPPER.writeValueAsString(dto)); | ||
} | ||
|
||
@Test | ||
void writeValueAsString_when_userDto_then_correctJson() throws JsonProcessingException { | ||
final UserDto userDto = | ||
UserDto.builder() | ||
.setId("user-id") | ||
.setType("type") | ||
.setUsername("user-name") | ||
.andAllOptionals() | ||
.setAge(25) | ||
.setEmail(Tristate.ofNull()) | ||
.build(); | ||
final AdminOrUserDiscriminatorMappingDto dto = | ||
AdminOrUserDiscriminatorMappingDto.builder().setUserDto(userDto).build(); | ||
|
||
assertEquals( | ||
"{\"age\":25,\"email\":null,\"id\":\"user-id\",\"type\":\"usr\",\"username\":\"user-name\"}", | ||
MAPPER.writeValueAsString(dto)); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...test/java/com/github/muehmar/gradle/openapi/oneof/DiscriminatorMappingValidationTest.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,94 @@ | ||
package com.github.muehmar.gradle.openapi.oneof; | ||
|
||
import static com.github.muehmar.gradle.openapi.util.ValidationUtil.validate; | ||
import static com.github.muehmar.gradle.openapi.util.ViolationFormatter.formatViolations; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.muehmar.gradle.openapi.util.MapperFactory; | ||
import java.util.Arrays; | ||
import java.util.Set; | ||
import javax.validation.ConstraintViolation; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class DiscriminatorMappingValidationTest { | ||
|
||
private static final ObjectMapper MAPPER = MapperFactory.mapper(); | ||
|
||
@Test | ||
void validate_when_matchesUserSchema_then_noViolation() throws JsonProcessingException { | ||
final AdminOrUserDiscriminatorMappingDto adminOrUserDto = | ||
MAPPER.readValue( | ||
"{\"id\":\"user-id\",\"type\":\"usr\",\"username\":\"user-name\",\"age\":25,\"email\":null}", | ||
AdminOrUserDiscriminatorMappingDto.class); | ||
|
||
final Set<ConstraintViolation<AdminOrUserDiscriminatorMappingDto>> violations = | ||
validate(adminOrUserDto); | ||
|
||
assertEquals(0, violations.size()); | ||
assertTrue(adminOrUserDto.isValid()); | ||
} | ||
|
||
@Test | ||
void validate_when_matchesUserSchemaButInvalidAge_then_violation() | ||
throws JsonProcessingException { | ||
final AdminOrUserDiscriminatorMappingDto adminOrUserDto = | ||
MAPPER.readValue( | ||
"{\"id\":\"user-id\",\"type\":\"usr\",\"username\":\"user-name\",\"age\":200,\"email\":null}", | ||
AdminOrUserDiscriminatorMappingDto.class); | ||
|
||
final Set<ConstraintViolation<AdminOrUserDiscriminatorMappingDto>> violations = | ||
validate(adminOrUserDto); | ||
|
||
assertEquals( | ||
Arrays.asList( | ||
"invalidOneOf[User].ageRaw -> must be less than or equal to 199", | ||
"validAgainstNoOneOfSchema -> Is not valid against one of the schemas [Admin, User]", | ||
"validAgainstTheCorrectSchema -> Not valid against the schema described by the discriminator"), | ||
formatViolations(violations), | ||
String.join("\n", formatViolations(violations))); | ||
assertFalse(adminOrUserDto.isValid()); | ||
} | ||
|
||
@Test | ||
void validate_when_matchesNoSchema_then_violations() throws JsonProcessingException { | ||
final AdminOrUserDiscriminatorMappingDto adminOrUserDto = | ||
MAPPER.readValue("{}", AdminOrUserDiscriminatorMappingDto.class); | ||
|
||
final Set<ConstraintViolation<AdminOrUserDiscriminatorMappingDto>> violations = | ||
validate(adminOrUserDto); | ||
|
||
assertEquals( | ||
Arrays.asList( | ||
"invalidOneOf[Admin].adminname -> must not be null", | ||
"invalidOneOf[Admin].id -> must not be null", | ||
"invalidOneOf[Admin].type -> must not be null", | ||
"invalidOneOf[User].id -> must not be null", | ||
"invalidOneOf[User].type -> must not be null", | ||
"invalidOneOf[User].username -> must not be null", | ||
"validAgainstNoOneOfSchema -> Is not valid against one of the schemas [Admin, User]", | ||
"validAgainstTheCorrectSchema -> Not valid against the schema described by the discriminator"), | ||
formatViolations(violations)); | ||
assertFalse(adminOrUserDto.isValid()); | ||
} | ||
|
||
@Test | ||
void validate_when_doesMatchBothSchemas_then_violation() throws JsonProcessingException { | ||
final AdminOrUserDiscriminatorMappingDto adminOrUserDto = | ||
MAPPER.readValue( | ||
"{\"id\":\"id-123\",\"type\":\"usr\",\"username\":\"user-name\",\"adminname\":\"admin-name\",\"age\":25,\"email\":null}", | ||
AdminOrUserDiscriminatorMappingDto.class); | ||
|
||
final Set<ConstraintViolation<AdminOrUserDiscriminatorMappingDto>> violations = | ||
validate(adminOrUserDto); | ||
|
||
assertEquals( | ||
Arrays.asList( | ||
"validAgainstMoreThanOneSchema -> Is valid against more than one of the schemas [Admin, User]"), | ||
formatViolations(violations)); | ||
assertFalse(adminOrUserDto.isValid()); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...test/java/com/github/muehmar/gradle/openapi/oneof/__snapshots__/GeneratedClassesTest.snap
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
Oops, something went wrong.