Skip to content

Commit

Permalink
Swagger 2.2.22 (micronaut-projects#1569)
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 authored May 17, 2024
1 parent 9ee3487 commit 8d4a718
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
managed-swagger = "2.2.21"
managed-swagger = "2.2.22"
managed-javadoc-parser = "0.3.1"
managed-jsoup = "1.17.2"
managed-html2md-converter = "0.64.8"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -68,13 +70,23 @@ public Schema createProperty() {
},
BYTE(Byte.class, "byte") {
@Override
public ByteArraySchema createProperty() {
public Schema createProperty() {
if (
(System.getProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY) != null && System.getProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY).equals(Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString())) ||
(System.getenv(Schema.BINARY_STRING_CONVERSION_PROPERTY) != null && System.getenv(Schema.BINARY_STRING_CONVERSION_PROPERTY).equals(Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString()))) {
return new StringSchema().format("byte");
}
return new ByteArraySchema();
}
},
BINARY(Byte.class, "binary") {
@Override
public BinarySchema createProperty() {
public Schema createProperty() {
if (
(System.getProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY) != null && System.getProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY).equals(Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString())) ||
(System.getenv(Schema.BINARY_STRING_CONVERSION_PROPERTY) != null && System.getenv(Schema.BINARY_STRING_CONVERSION_PROPERTY).equals(Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString()))) {
return new StringSchema().format("binary");
}
return new BinarySchema();
}
},
Expand Down Expand Up @@ -176,6 +188,7 @@ public Schema createProperty() {
};

private static final Map<Class<?>, PrimitiveType> KEY_CLASSES;
private static final Map<Class<?>, Collection<PrimitiveType>> MULTI_KEY_CLASSES;
private static final Map<Class<?>, PrimitiveType> BASE_CLASSES;
/**
* Adds support of a small number of "well-known" types, specifically for
Expand Down Expand Up @@ -269,6 +282,11 @@ public Schema createProperty() {
addKeys(keyClasses, OBJECT, Object.class);
KEY_CLASSES = Collections.unmodifiableMap(keyClasses);

final Map<Class<?>, Collection<PrimitiveType>> multiKeyClasses = new HashMap<>();
addMultiKeys(multiKeyClasses, BYTE, byte[].class);
addMultiKeys(multiKeyClasses, BINARY, byte[].class);
MULTI_KEY_CLASSES = Collections.unmodifiableMap(multiKeyClasses);

final Map<Class<?>, PrimitiveType> baseClasses = new HashMap<>();
addKeys(baseClasses, DATE_TIME, Date.class, Calendar.class);
BASE_CLASSES = Collections.unmodifiableMap(baseClasses);
Expand Down Expand Up @@ -374,13 +392,35 @@ public static Set<String> nonSystemTypePackages() {
return nonSystemTypePackages;
}

public static PrimitiveType fromTypeAndFormat(Type type, String format) {
final Class<?> raw = TypeFactory.defaultInstance().constructType(type).getRawClass();
final Collection<PrimitiveType> keys = MULTI_KEY_CLASSES.get(raw);
if (keys == null || keys.isEmpty() || format == null || format.isBlank()) {
return fromType(type);
} else {
return keys
.stream()
.filter(t -> t.getCommonName().equalsIgnoreCase(format))
.findAny()
.orElse(null);
}
}

public static PrimitiveType fromType(Type type) {
final Class<?> raw = TypeFactory.defaultInstance().constructType(type).getRawClass();
final PrimitiveType key = KEY_CLASSES.get(raw);
if (key != null && !customExcludedClasses.contains(raw.getName())) {
return key;
}

final Collection<PrimitiveType> keys = MULTI_KEY_CLASSES.get(raw);
if (keys != null && !keys.isEmpty()) {
final PrimitiveType first = keys.iterator().next();
if (!customExcludedClasses.contains(raw.getName())) {
return first;
}
}

final PrimitiveType custom = customClasses.get(raw.getName());
if (custom != null) {
return custom;
Expand Down Expand Up @@ -451,6 +491,16 @@ private static <K> void addKeys(Map<K, PrimitiveType> map, PrimitiveType type, K
}
}

@SafeVarargs
private static <K> void addMultiKeys(Map<K, Collection<PrimitiveType>> map, PrimitiveType type, K... keys) {
for (K key : keys) {
if (!map.containsKey(key)) {
map.put(key, new ArrayList<>());
}
map.get(key).add(type);
}
}

private static class DateStub {

}
Expand Down

0 comments on commit 8d4a718

Please sign in to comment.