Skip to content

Commit

Permalink
Fix process operation parameters.
Browse files Browse the repository at this point in the history
Fix support basic java types
  • Loading branch information
altro3 committed Sep 4, 2024
1 parent a011b63 commit 579b76a
Show file tree
Hide file tree
Showing 8 changed files with 718 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,51 @@
*/
package io.micronaut.openapi.swagger.core.util;

import com.fasterxml.jackson.databind.type.TypeFactory;
import io.swagger.v3.oas.models.media.BinarySchema;
import io.swagger.v3.oas.models.media.BooleanSchema;
import io.swagger.v3.oas.models.media.ByteArraySchema;
import io.swagger.v3.oas.models.media.DateSchema;
import io.swagger.v3.oas.models.media.DateTimeSchema;
import io.swagger.v3.oas.models.media.FileSchema;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.NumberSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.media.UUIDSchema;

import java.io.File;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;

import io.swagger.v3.oas.models.media.BinarySchema;
import io.swagger.v3.oas.models.media.BooleanSchema;
import io.swagger.v3.oas.models.media.ByteArraySchema;
import io.swagger.v3.oas.models.media.DateSchema;
import io.swagger.v3.oas.models.media.DateTimeSchema;
import io.swagger.v3.oas.models.media.FileSchema;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.NumberSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.media.UUIDSchema;

import com.fasterxml.jackson.databind.type.TypeFactory;

import static java.util.Map.entry;

/**
Expand All @@ -58,53 +73,53 @@
public enum PrimitiveType {
STRING(String.class, "string") {
@Override
public Schema createProperty() {
public Schema<?> createProperty() {
return new StringSchema();
}
},
BOOLEAN(Boolean.class, "boolean") {
@Override
public Schema createProperty() {
public Schema<?> createProperty() {
return new BooleanSchema();
}
},
BYTE(Byte.class, "byte") {
@Override
public Schema 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()))) {
(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 Schema 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()))) {
(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();
}
},
URI(java.net.URI.class, "uri") {
@Override
public Schema createProperty() {
public Schema<?> createProperty() {
return new StringSchema().format("uri");
}
},
URL(java.net.URL.class, "url") {
@Override
public Schema createProperty() {
public Schema<?> createProperty() {
return new StringSchema().format("url");
}
},
EMAIL(String.class, "email") {
@Override
public Schema createProperty() {
public Schema<?> createProperty() {
return new StringSchema().format("email");
}
},
Expand All @@ -122,37 +137,37 @@ public IntegerSchema createProperty() {
},
LONG(Long.class, "long") {
@Override
public Schema createProperty() {
public Schema<?> createProperty() {
return new IntegerSchema().format("int64");
}
},
FLOAT(Float.class, "float") {
@Override
public Schema createProperty() {
public Schema<?> createProperty() {
return new NumberSchema().format("float");
}
},
DOUBLE(Double.class, "double") {
@Override
public Schema createProperty() {
public Schema<?> createProperty() {
return new NumberSchema().format("double");
}
},
INTEGER(BigInteger.class) {
@Override
public Schema createProperty() {
public Schema<?> createProperty() {
return new IntegerSchema().format(null);
}
},
DECIMAL(BigDecimal.class, "number") {
@Override
public Schema createProperty() {
public Schema<?> createProperty() {
return new NumberSchema();
}
},
NUMBER(Number.class, "number") {
@Override
public Schema createProperty() {
public Schema<?> createProperty() {
return new NumberSchema();
}
},
Expand All @@ -170,7 +185,7 @@ public DateTimeSchema createProperty() {
},
PARTIAL_TIME(LocalTime.class, "partial-time") {
@Override
public Schema createProperty() {
public Schema<?> createProperty() {
return new StringSchema().format("partial-time");
}
},
Expand All @@ -182,8 +197,8 @@ public FileSchema createProperty() {
},
OBJECT(Object.class) {
@Override
public Schema createProperty() {
return new Schema().type("object");
public Schema<?> createProperty() {
return new Schema<>().type("object");
}
};

Expand Down Expand Up @@ -238,7 +253,6 @@ public Schema createProperty() {
static {
systemPrefixes.add("java.");
systemPrefixes.add("javax.");
nonSystemTypes.add("java.time.LocalTime");

datatypeMappings = Map.ofEntries(
entry("integer_int32", "integer"),
Expand All @@ -262,9 +276,9 @@ public Schema createProperty() {
entry("object_", "object")
);

final Map<Class<?>, PrimitiveType> keyClasses = new HashMap<>();
final var keyClasses = new HashMap<Class<?>, PrimitiveType>();
addKeys(keyClasses, BOOLEAN, Boolean.class, Boolean.TYPE);
addKeys(keyClasses, STRING, String.class, Character.class, Character.TYPE);
addKeys(keyClasses, STRING, String.class, Character.class, CharSequence.class, Character.TYPE);
addKeys(keyClasses, BYTE, Byte.class, Byte.TYPE);
addKeys(keyClasses, URL, java.net.URL.class);
addKeys(keyClasses, URI, java.net.URI.class);
Expand All @@ -280,31 +294,48 @@ public Schema createProperty() {
addKeys(keyClasses, DATE_TIME, Date.class);
addKeys(keyClasses, FILE, File.class);
addKeys(keyClasses, OBJECT, Object.class);

// OpenAPI specification haven't 'format' for these 'java.time' classes and other 'java.util' classes
addKeys(keyClasses, STRING,
Duration.class,
Period.class,
LocalTime.class,
OffsetTime.class,
YearMonth.class,
Year.class,
MonthDay.class,
ZoneId.class,
ZoneOffset.class,
TimeZone.class,
Charset.class,
Locale.class
);

KEY_CLASSES = Collections.unmodifiableMap(keyClasses);

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

final Map<Class<?>, PrimitiveType> baseClasses = new HashMap<>();
final var baseClasses = new HashMap<Class<?>, PrimitiveType>();
addKeys(baseClasses, DATE_TIME, Date.class, Calendar.class);
BASE_CLASSES = Collections.unmodifiableMap(baseClasses);

final Map<String, PrimitiveType> externalClasses = new HashMap<>();
addKeys(externalClasses, DATE, "org.joda.time.LocalDate", "java.time.LocalDate");
final var externalClasses = new HashMap<String, PrimitiveType>();
addKeys(externalClasses, DATE, "org.joda.time.LocalDate", LocalDate.class.getName());
addKeys(externalClasses, DATE_TIME,
"java.time.LocalDateTime",
"java.time.ZonedDateTime",
"java.time.OffsetDateTime",
LocalDateTime.class.getName(),
ZonedDateTime.class.getName(),
OffsetDateTime.class.getName(),
Instant.class.getName(),
"javax.xml.datatype.XMLGregorianCalendar",
"org.joda.time.LocalDateTime",
"org.joda.time.ReadableDateTime",
"org.joda.time.DateTime",
"java.time.Instant");
"org.joda.time.DateTime");
EXTERNAL_CLASSES = Collections.unmodifiableMap(externalClasses);

final Map<String, PrimitiveType> names = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
var names = new TreeMap<String, PrimitiveType>(String.CASE_INSENSITIVE_ORDER);
for (PrimitiveType item : values()) {
final String name = item.commonName;
if (name != null) {
Expand All @@ -330,7 +361,6 @@ public Schema createProperty() {
* Adds support for custom mapping of classes to primitive types
*
* @return Set of custom classes to primitive type
*
* @since 2.0.6
*/
public static Set<String> customExcludedClasses() {
Expand All @@ -341,7 +371,6 @@ public static Set<String> customExcludedClasses() {
* Adds support for custom mapping of classes to primitive types
*
* @return Set of custom classes to primitive type
*
* @since 2.1.2
*/
public static Set<String> customExcludedExternalClasses() {
Expand All @@ -352,7 +381,6 @@ public static Set<String> customExcludedExternalClasses() {
* Adds support for custom mapping of classes to primitive types
*
* @return Map of custom classes to primitive type
*
* @since 2.0.6
*/
public static Map<String, PrimitiveType> customClasses() {
Expand All @@ -363,7 +391,6 @@ public static Map<String, PrimitiveType> customClasses() {
* class qualified names prefixes to be considered as "system" types
*
* @return Mutable set of class qualified names prefixes to be considered as "system" types
*
* @since 2.0.6
*/
public static Set<String> systemPrefixes() {
Expand All @@ -374,7 +401,6 @@ public static Set<String> systemPrefixes() {
* class qualified names NOT to be considered as "system" types
*
* @return Mutable set of class qualified names NOT to be considered as "system" types
*
* @since 2.0.6
*/
public static Set<String> nonSystemTypes() {
Expand All @@ -385,7 +411,6 @@ public static Set<String> nonSystemTypes() {
* package names NOT to be considered as "system" types
*
* @return Mutable set of package names NOT to be considered as "system" types
*
* @since 2.0.6
*/
public static Set<String> nonSystemTypePackages() {
Expand All @@ -399,10 +424,10 @@ public static PrimitiveType fromTypeAndFormat(Type type, String format) {
return fromType(type);
} else {
return keys
.stream()
.filter(t -> t.getCommonName().equalsIgnoreCase(format))
.findAny()
.orElse(null);
.stream()
.filter(t -> t.getCommonName().equalsIgnoreCase(format))
.findAny()
.orElse(null);
}
}

Expand Down Expand Up @@ -457,12 +482,12 @@ public static PrimitiveType fromTypeAndFormat(String type, String format) {
return fromName(datatypeMappings.get(String.format("%s_%s", type != null && !type.isBlank() ? type : "", format != null && !format.isBlank() ? format : "")));
}

public static Schema createProperty(Type type) {
public static Schema<?> createProperty(Type type) {
final PrimitiveType item = fromType(type);
return item == null ? null : item.createProperty();
}

public static Schema createProperty(String name) {
public static Schema<?> createProperty(String name) {
final PrimitiveType item = fromName(name);
return item == null ? null : item.createProperty();
}
Expand All @@ -480,7 +505,7 @@ public String getCommonName() {
return commonName;
}

public abstract Schema createProperty();
public abstract Schema<?> createProperty();

@SafeVarargs
private static <K> void addKeys(Map<K, PrimitiveType> map, PrimitiveType type, K... keys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
}

/**
* This method removes all implicit header parameters from the list of parameters
* This method removes all implicit header parameters from the list of parameters.
*
* @param operation - operation to be processed
*/
Expand Down
Loading

0 comments on commit 579b76a

Please sign in to comment.