Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
muehmar committed Oct 6, 2024
1 parent b988766 commit 8aa89ac
Show file tree
Hide file tree
Showing 46 changed files with 2,006 additions and 62 deletions.
15 changes: 14 additions & 1 deletion example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def specifications =
[name: 'sasis', file: 'sasis-api.json'],
[name: 'promotion', file: 'openapi-promotion.yml'],
[name: 'overrideglobalconfig', file: 'openapi-override-global-config.yml'],
[name: 'nullableitemslist', file: 'openapi-nullable-items-list.yml']
[name: 'nullableitemslist', file: 'openapi-nullable-items-list.yml'],
[name: 'typemappingwithconversion', file: 'openapi-type-mapping-with-conversion.yml']
]

def issueNumbers = [
Expand All @@ -92,6 +93,7 @@ def issueNumbers = [
'272',
'275',
'280',
'278',
'287']

specifications.addAll(issueNumbers.collect { issueNumber -> [name: "Issue${issueNumber}", file: "issues/openapi-issue-${issueNumber}.yml"] })
Expand Down Expand Up @@ -282,6 +284,17 @@ openApiGenerator {
inputSpec = "$projectDir/src/main/resources/openapi-nullable-items-list.yml"
packageName = "com.github.muehmar.gradle.openapi.nullableitemslist"
}
typeMappingWithConversion {
inputSpec = "$projectDir/src/main/resources/openapi-type-mapping-with-conversion.yml"
packageName = "com.github.muehmar.gradle.openapi.typemappingwithconversion"

classMapping {
fromClass = "String"
toClass = "com.github.muehmar.gradle.openapi.typemappingwithconversion.CustomString"
conversion.fromCustomType = "getValue"
conversion.toCustomType = "CustomString#fromString"
}
}
issueNumbers.forEach { issueNumber ->
{
"Issue${issueNumber}" {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.muehmar.gradle.openapi.typemappingwithconversion;

import java.util.Objects;

public class CustomString {
private final String value;

public static CustomString fromString(String value) {
return new CustomString(value);
}

public CustomString(String value) {
this.value = value;
}

public String getValue() {
return value;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final CustomString that = (CustomString) o;
return Objects.equals(value, that.value);
}

@Override
public int hashCode() {
return Objects.hashCode(value);
}

@Override
public String toString() {
return "CustomString{" + "value='" + value + '\'' + '}';
}
}
54 changes: 54 additions & 0 deletions example/src/main/resources/issues/openapi-issue-278.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
openapi: "3.0.0"
info: { }

paths: { }

components:
schemas:

FullObject:
oneOf:
- $ref: '#/components/schemas/Admin'
- $ref: '#/components/schemas/User'

NestedFullObject:
required:
- amount
anyOf:
- $ref: '#/components/schemas/FullObject'
- $ref: '#/components/schemas/Member'
properties:
amount:
type: integer

User:
required:
- type
- username
properties:
type:
type: string
username:
type: string

Admin:
required:
- type
- adminname
properties:
type:
type: string
adminname:
type: string
nullable: true

Member:
required:
- type
- membername
properties:
type:
type: string
membername:
type: string

178 changes: 178 additions & 0 deletions example/src/main/resources/openapi-type-mapping-with-conversion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
openapi: "3.0.0"
info: { }

paths: { }

components:
schemas:
User:
required:
- id
- username
properties:
id:
type: string
minLength: 6
maxLength: 10
username:
type: string
nullable: true
minLength: 5
maxLength: 20
email:
type: string
pattern: '[A-Za-z0-9]+@[A-Za-z0-9]+\.[a-z]+'
phone:
type: string
nullable: true
pattern: '\+41[0-9]{7}'
additionalProperties:
type: string
nullable: true

ListObject:
required:
- ids
- usernames
properties:
ids:
type: array
items:
type: string
nullable: true
minLength: 6
maxLength: 10
minItems: 1
maxItems: 2
usernames:
type: array
items:
type: string
nullable: true
minLength: 9
maxLength: 12
nullable: true
minItems: 3
maxItems: 4
emails:
type: array
items:
type: string
nullable: true
pattern: '[a-z]+-[0-9]{4}'
minItems: 5
maxItems: 6
phones:
type: array
items:
type: string
nullable: true
pattern: 'phone-[0-9]{4}'
minItems: 7
maxItems: 8
nullable: true
additionalProperties:
type: string

MapObject:
type: object
required:
- idsMap
- usernamesMap
properties:
idsMap:
additionalProperties:
type: string
minLength: 6
maxLength: 10
usernamesMap:
nullable: true
additionalProperties:
type: string
nullable: true
minLength: 9
maxLength: 12
emailsMap:
additionalProperties:
type: string
nullable: true
pattern: '[a-z]+-[0-9]{4}'
phonesMap:
nullable: true
additionalProperties:
type: string
nullable: true
pattern: 'phone-[0-9]{4}'

AllOfListObject:
allOf:
- $ref: '#/components/schemas/ListObject'
- type: object
properties:
superUserId:
type: string

OneOfListObject:
oneOf:
- $ref: '#/components/schemas/ListObject'
- type: object
properties:
superUserId:
type: string

AnyOfListObject:
anyOf:
- $ref: '#/components/schemas/ListObject'
- type: object
properties:
superUserId:
type: string

AllOfMapObject:
allOf:
- $ref: '#/components/schemas/MapObject'
- type: object
properties:
superUserId:
type: string

OneOfMapObject:
oneOf:
- $ref: '#/components/schemas/MapObject'
- type: object
properties:
superUserId:
type: string

AnyOfMapObject:
anyOf:
- $ref: '#/components/schemas/MapObject'
- type: object
properties:
superUserId:
type: string

Posology:
description: |
Doses to be taken (morning, midday, evening, night). Used for
package size calculation.
type: array
maxItems: 4
items:
type: string
minLength: 1
example:
- "1"
- "0"
- "2"
- "0"

ArrayAdditionalProperties:
type: object
additionalProperties:
type: array
items:
type: string



13 changes: 13 additions & 0 deletions example/src/test/java/com/github/muehmar/gradle/openapi/Lists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.muehmar.gradle.openapi;

import java.util.Arrays;
import java.util.List;

public class Lists {
private Lists() {}

@SafeVarargs
public static <T> List<T> list(T... elements) {
return Arrays.asList(elements);
}
}
20 changes: 20 additions & 0 deletions example/src/test/java/com/github/muehmar/gradle/openapi/Maps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.muehmar.gradle.openapi;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Maps {
private Maps() {}

public static <K, V> Map<K, V> map(K key, V value) {
return Collections.singletonMap(key, value);
}

public static <K, V> Map<K, V> map(K key1, V value1, K key2, V value2) {
final HashMap<K, V> map = new HashMap<>();
map.put(key1, value1);
map.put(key2, value2);
return Collections.unmodifiableMap(map);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.muehmar.gradle.openapi;

import java.util.Optional;

public class Optionals {
private Optionals() {}

public static <T> Optional<T> opt(T value) {
return Optional.of(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ void serialize_when_objectWithObjectAdditionalProperty_then_correctJson()
@Test
void deserialize_when_objectWithObjectAdditionalProperty_then_correctDto()
throws JsonProcessingException {
assertEquals(DTO, MAPPER.readValue(JSON, ObjectAdditionalPropertiesDto.class));
final ObjectAdditionalPropertiesDto actual =
MAPPER.readValue(JSON, ObjectAdditionalPropertiesDto.class);
assertEquals(DTO, actual);
}

@Test
Expand Down Expand Up @@ -75,7 +77,7 @@ void validate_when_descriptionInAdditionalPropertiesObjectTooShort_then_violatio
"size must be between 5 and 2147483647",
violations.stream().findFirst().get().getMessage());
assertEquals(
"additionalProperties_[prop1].descriptionRaw",
"additionalProperties_[prop1].description",
violations.stream().findFirst().get().getPropertyPath().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void validate_when_matchesUserSchemaButInvalidAge_then_violation()

assertEquals(
Arrays.asList(
"invalidAnyOf[User].ageRaw -> must be less than or equal to 199",
"invalidAnyOf[User].age -> must be less than or equal to 199",
"validAgainstNoAnyOfSchema -> Is not valid against one of the schemas [Admin, User]",
"validAgainstTheCorrectAnyOfSchema -> Not valid against the schema described by the anyOf-discriminator"),
formatViolations(violations),
Expand Down
Loading

0 comments on commit 8aa89ac

Please sign in to comment.