Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Issue: #160
  • Loading branch information
muehmar committed Oct 8, 2024
1 parent af4979b commit 323cc40
Show file tree
Hide file tree
Showing 45 changed files with 2,004 additions and 63 deletions.
37 changes: 35 additions & 2 deletions 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 All @@ -118,11 +120,15 @@ openApiGenerator {
formatTypeMapping {
formatType = "username"
classType = "com.package.UserName"
conversion.fromCustomType = "getValue"
conversion.toCustomType = "UserName#fromString"
}

formatTypeMapping {
formatType = "password"
classType = "com.package.Password"
conversion.fromCustomType = "getValue"
conversion.toCustomType = "Password#fromString"
}

validationMethods {
Expand All @@ -133,6 +139,7 @@ openApiGenerator {
disableWarnings = false
failOnWarnings = true
failOnUnsupportedValidation = true
failOnMissingMappingConversion = true
}

constantSchemaNameMapping {
Expand Down Expand Up @@ -163,6 +170,10 @@ openApiGenerator {
fromClass = "List"
toClass = "java.util.ArrayList"
}

warnings {
failOnMissingMappingConversion = false
}
}
apiV2 {
inputSpec = "$projectDir/src/main/resources/openapi-v2.yml"
Expand Down Expand Up @@ -215,6 +226,7 @@ openApiGenerator {

warnings {
failOnUnsupportedValidation = false
failOnMissingMappingConversion = false
}
}
oneOf {
Expand Down Expand Up @@ -258,6 +270,10 @@ openApiGenerator {
formatType = "date-time"
classType = "java.time.LocalDate"
}

warnings {
failOnMissingMappingConversion = false
}
}
promotion {
inputSpec = "$projectDir/src/main/resources/openapi-promotion.yml"
Expand All @@ -272,7 +288,6 @@ openApiGenerator {
jsonSupport = "none"
enableValidation = false
builderMethodPrefix = ""

}
oneOfEnumDiscriminator {
inputSpec = "$projectDir/src/main/resources/openapi-oneof-enum-discriminator.yml"
Expand All @@ -282,6 +297,24 @@ 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"
}

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

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

public class CustomList<T> {
private final List<T> list;

public CustomList(List<T> list) {
this.list = list;
}

public static <T> CustomList<T> fromList(List<T> list) {
return new CustomList<>(list);
}

public static <T> CustomList<T> emptyCustomList() {
return customList();
}

@SafeVarargs
public static <T> CustomList<T> customList(T... items) {
return new CustomList<>(Arrays.asList(items));
}

public List<T> asList() {
return list;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final CustomList<?> that = (CustomList<?>) o;
return Objects.equals(list, that.list);
}

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

@Override
public String toString() {
return "CustomList{" + "list=" + list + '}';
}
}
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

Loading

0 comments on commit 323cc40

Please sign in to comment.