From 79ba4fb59ead32965ba3fc97337d6779fad5c998 Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Tue, 16 Jul 2024 13:47:32 +0200 Subject: [PATCH] refactor: OpenAPI-based generator and Maven Plugin Java 8 compatible Signed-off-by: Marc Nuri --- .../openapi/maven-plugin/pom.xml | 6 +- .../schema/generator/ApiVersion.java | 2 +- .../schema/generator/GeneratorUtils.java | 9 +- .../generator/ImportOrderComparator.java | 4 +- .../schema/generator/SchemaUtils.java | 161 ++++++++++-------- .../generator/model/ModelGenerator.java | 4 +- .../schema/generator/ApiVersionTest.java | 2 +- .../schema/generator/SchemaUtilsTest.java | 79 ++++----- .../generator/model/TemplateContextTest.java | 19 ++- 9 files changed, 158 insertions(+), 128 deletions(-) diff --git a/kubernetes-model-generator/openapi/maven-plugin/pom.xml b/kubernetes-model-generator/openapi/maven-plugin/pom.xml index 3207abfdc34..39941b7f2e3 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/pom.xml +++ b/kubernetes-model-generator/openapi/maven-plugin/pom.xml @@ -32,9 +32,9 @@ maven-plugin - 11 - 11 - 11 + + + true diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/ApiVersion.java b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/ApiVersion.java index 45801419a66..87a5a41faca 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/ApiVersion.java +++ b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/ApiVersion.java @@ -27,7 +27,7 @@ public class ApiVersion { @Override public String toString() { - if (group == null || group.isBlank()) { + if (group == null || group.trim().isEmpty()) { return version; } return group + "/" + version; diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/GeneratorUtils.java b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/GeneratorUtils.java index f1052b703ac..6b3e07868aa 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/GeneratorUtils.java +++ b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/GeneratorUtils.java @@ -17,9 +17,11 @@ package io.fabric8.kubernetes.schema.generator; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -56,8 +58,9 @@ public GeneratorUtils(GeneratorSettings settings) { } public String readTemplate(String name) { - try (var stream = SchemaUtils.class.getResourceAsStream("/templates/" + name + ".mustache")) { - return new String(Objects.requireNonNull(stream).readAllBytes(), StandardCharsets.UTF_8); + try (InputStream stream = Objects + .requireNonNull(SchemaUtils.class.getResourceAsStream("/templates/" + name + ".mustache"))) { + return IOUtils.toString(stream, StandardCharsets.UTF_8); } catch (IOException ex) { settings.getLogger().severe(ex.getMessage()); throw new GeneratorException("Can't load template " + name); @@ -66,7 +69,7 @@ public String readTemplate(String name) { public final void writeFile(Path file, String fileContents) { try { - Files.writeString(file, fileContents, + Files.write(file, fileContents.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); } catch (IOException ex) { settings.getLogger().severe(ex.getMessage()); diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/ImportOrderComparator.java b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/ImportOrderComparator.java index 8755e96b05a..4800e827b7b 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/ImportOrderComparator.java +++ b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/ImportOrderComparator.java @@ -15,11 +15,13 @@ */ package io.fabric8.kubernetes.schema.generator; +import java.util.Arrays; import java.util.Comparator; +import java.util.HashSet; import java.util.Set; public class ImportOrderComparator implements Comparator { - private static final Set JAVA_PACKAGES = Set.of("java", "javax"); + private static final Set JAVA_PACKAGES = new HashSet<>(Arrays.asList("java", "javax")); @Override public int compare(String o1, String o2) { diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/SchemaUtils.java b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/SchemaUtils.java index ee789d834c4..7c0e9b3ada5 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/SchemaUtils.java +++ b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/SchemaUtils.java @@ -24,7 +24,10 @@ import io.swagger.v3.oas.models.media.StringSchema; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; import java.util.Objects; @@ -40,64 +43,82 @@ public class SchemaUtils { private static final String OBJECT_PRIMITIVE = "Object"; private static final String STRING_PRIMITIVE = "String"; - private static final Map REF_TO_JAVA_TYPE_MAP = Map.of( - "#/components/schemas/io.k8s.apimachinery.pkg.util.intstr.IntOrString", "io.fabric8.kubernetes.api.model.IntOrString", - "#/components/schemas/io.k8s.apimachinery.pkg.api.resource.Quantity", "io.fabric8.kubernetes.api.model.Quantity", - "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta", "io.fabric8.kubernetes.api.model.ObjectMeta", - "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta_v2", "io.fabric8.kubernetes.api.model.ObjectMeta", - "#/components/schemas/io.k8s.apimachinery.pkg.runtime.RawExtension", "io.fabric8.kubernetes.api.model.KubernetesResource", - "#/components/schemas/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSON", - "com.fasterxml.jackson.databind.JsonNode", - "#/components/schemas/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1.JSON", - "com.fasterxml.jackson.databind.JsonNode"); - - private static final Map REF_TO_JAVA_PRIMITIVE_MAP = Map.of( - "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.Time", "String"); - - private static final Map JAVA_CLASS_SERIALIZER_MAP = Map.of( - "io.fabric8.kubernetes.api.model.MicroTime", "io.fabric8.kubernetes.api.model.MicroTimeSerDes.Serializer.class", - "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrArray", - "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrArraySerDe.Serializer.class", - "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrArray", - "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrArraySerDe.Serializer.class", - "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrBool", - "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrBoolSerDe.Serializer.class", - "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrBool", - "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrBoolSerDe.Serializer.class", - "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrStringArray", - "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrStringArraySerDe.Serializer.class", - "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrStringArray", - "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrStringArraySerDe.Serializer.class"); - - private static final Map JAVA_CLASS_DESERIALIZER_MAP = Map.of( - "io.fabric8.kubernetes.api.model.MicroTime", "io.fabric8.kubernetes.api.model.MicroTimeSerDes.Deserializer.class", - "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrArray", - "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrArraySerDe.Deserializer.class", - "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrArray", - "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrArraySerDe.Deserializer.class", - "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrBool", - "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrBoolSerDe.Deserializer.class", - "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrBool", - "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrBoolSerDe.Deserializer.class", - "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrStringArray", - "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrStringArraySerDe.Deserializer.class", - "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrStringArray", - "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrStringArraySerDe.Deserializer.class"); - - private static final Map REF_SERIALIZER_MAP = Map.of( - // "#/components/schemas/io.k8s.apimachinery.pkg.util.intstr.IntOrString", "com.marcnuri.yakc.model.serialization.IntOrStringSerializer.class" - ); - - private static final Map TYPE_MAP = Map.of( - "boolean", "Boolean", - "int32", "Integer", - "int64", "Long", - "double", "Double", - "number", "Number", - "object", OBJECT_PRIMITIVE, - "string", STRING_PRIMITIVE); - - private static final Set PROTECTED_WORDS = Set.of( + private static final Map REF_TO_JAVA_TYPE_MAP = new LinkedHashMap<>(); + static { + REF_TO_JAVA_TYPE_MAP.put("#/components/schemas/io.k8s.apimachinery.pkg.util.intstr.IntOrString", + "io.fabric8.kubernetes.api.model.IntOrString"); + REF_TO_JAVA_TYPE_MAP.put("#/components/schemas/io.k8s.apimachinery.pkg.api.resource.Quantity", + "io.fabric8.kubernetes.api.model.Quantity"); + REF_TO_JAVA_TYPE_MAP.put("#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta", + "io.fabric8.kubernetes.api.model.ObjectMeta"); + REF_TO_JAVA_TYPE_MAP.put("#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta_v2", + "io.fabric8.kubernetes.api.model.ObjectMeta"); + REF_TO_JAVA_TYPE_MAP.put("#/components/schemas/io.k8s.apimachinery.pkg.runtime.RawExtension", + "io.fabric8.kubernetes.api.model.KubernetesResource"); + REF_TO_JAVA_TYPE_MAP.put("#/components/schemas/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSON", + "com.fasterxml.jackson.databind.JsonNode"); + REF_TO_JAVA_TYPE_MAP.put("#/components/schemas/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1.JSON", + "com.fasterxml.jackson.databind.JsonNode"); + } + + private static final Map REF_TO_JAVA_PRIMITIVE_MAP = new LinkedHashMap<>(); + static { + REF_TO_JAVA_PRIMITIVE_MAP.put("#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.Time", "String"); + } + + private static final Map JAVA_CLASS_SERIALIZER_MAP = new LinkedHashMap<>(); + static { + JAVA_CLASS_SERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.MicroTime", + "io.fabric8.kubernetes.api.model.MicroTimeSerDes.Serializer.class"); + JAVA_CLASS_SERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrArray", + "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrArraySerDe.Serializer.class"); + JAVA_CLASS_SERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrArray", + "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrArraySerDe.Serializer.class"); + JAVA_CLASS_SERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrBool", + "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrBoolSerDe.Serializer.class"); + JAVA_CLASS_SERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrBool", + "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrBoolSerDe.Serializer.class"); + JAVA_CLASS_SERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrStringArray", + "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrStringArraySerDe.Serializer.class"); + JAVA_CLASS_SERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrStringArray", + "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrStringArraySerDe.Serializer.class"); + } + + private static final Map JAVA_CLASS_DESERIALIZER_MAP = new LinkedHashMap<>(); + static { + JAVA_CLASS_DESERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.MicroTime", + "io.fabric8.kubernetes.api.model.MicroTimeSerDes.Deserializer.class"); + JAVA_CLASS_DESERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrArray", + "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrArraySerDe.Deserializer.class"); + JAVA_CLASS_DESERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrArray", + "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrArraySerDe.Deserializer.class"); + JAVA_CLASS_DESERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrBool", + "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrBoolSerDe.Deserializer.class"); + JAVA_CLASS_DESERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrBool", + "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrBoolSerDe.Deserializer.class"); + JAVA_CLASS_DESERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrStringArray", + "io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrStringArraySerDe.Deserializer.class"); + JAVA_CLASS_DESERIALIZER_MAP.put("io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrStringArray", + "io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrStringArraySerDe.Deserializer.class"); + } + + private static final Map REF_SERIALIZER_MAP = Collections.emptyMap();// new LinkedHashMap<>(); + static { + // REF_SERIALIZER_MAP.put("#/components/schemas/io.k8s.apimachinery.pkg.util.intstr.IntOrString", "com.marcnuri.yakc.model.serialization.IntOrStringSerializer.class"); + } + + private static final Map TYPE_MAP = new LinkedHashMap<>(); + static { + TYPE_MAP.put("boolean", "Boolean"); + TYPE_MAP.put("int32", "Integer"); + TYPE_MAP.put("int64", "Long"); + TYPE_MAP.put("double", "Double"); + TYPE_MAP.put("number", "Number"); + TYPE_MAP.put("object", OBJECT_PRIMITIVE); + TYPE_MAP.put("string", STRING_PRIMITIVE); + } + + private static final Set PROTECTED_WORDS = new HashSet<>(Arrays.asList( "continue", "default", "enum", @@ -107,7 +128,7 @@ public class SchemaUtils { "private", "for", "return", - "package"); + "package")); private final GeneratorSettings settings; @@ -168,16 +189,18 @@ public String schemaToClassName(Consumer addImport, Schema schema) { if (isString(schema)) { return "String"; } - if (ref != null && !ref.isBlank()) { - return schemaRefToJavaPrimitive(schema) - .or(() -> schemaRefToJavaType(schema).map(javaType -> { - addImport.accept(javaType); - return javaType.substring(javaType.lastIndexOf('.') + 1); - })) - .orElseGet(() -> { - addImport.accept(refToModelPackage(ref)); - return refToClassName(ref); - }); + if (ref != null && !ref.trim().isEmpty()) { + final Optional javaPrimitive = schemaRefToJavaPrimitive(schema); + if (javaPrimitive.isPresent()) { + return javaPrimitive.get(); + } + final Optional javaType = schemaRefToJavaType(schema); + if (javaType.isPresent()) { + addImport.accept(javaType.get()); + return javaType.get().substring(javaType.get().lastIndexOf('.') + 1); + } + addImport.accept(refToModelPackage(ref)); + return refToClassName(ref); } // Plain OpenAPI object map to KubernetesResource (deserializer will take care of the rest) if (isObject(schema)) { diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/model/ModelGenerator.java b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/model/ModelGenerator.java index 158e0ee24a2..0dc09a79e32 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/model/ModelGenerator.java +++ b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/model/ModelGenerator.java @@ -118,7 +118,7 @@ private void processTemplate(TemplateContext ret) { .orElse("com.fasterxml.jackson.databind.JsonDeserializer.None.class")); ret.put("package", ret.getPackageName()); if (settings.isGenerateJavadoc()) { - ret.put("hasDescription", !sanitizeDescription(ret.getClassSchema().getDescription()).isBlank()); + ret.put("hasDescription", !sanitizeDescription(ret.getClassSchema().getDescription()).trim().isEmpty()); ret.put("description", sanitizeDescription(ret.getClassSchema().getDescription())); } ret.addImport("com.fasterxml.jackson.annotation.JsonInclude"); @@ -162,7 +162,7 @@ private List> templateFields(TemplateContext templateContext templateProp.put("required", true); } if (settings.isGenerateJavadoc()) { - templateProp.put("hasDescription", !sanitizeDescription(propertySchema.getDescription()).isBlank()); + templateProp.put("hasDescription", !sanitizeDescription(propertySchema.getDescription()).trim().isEmpty()); templateProp.put("description", sanitizeDescription(propertySchema.getDescription())); } final String serializeUsing = serializerForSchema(propertySchema); diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/ApiVersionTest.java b/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/ApiVersionTest.java index 1e547afa1c3..0842ffd3858 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/ApiVersionTest.java +++ b/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/ApiVersionTest.java @@ -29,7 +29,7 @@ class ApiVersionTest { "group, v1, group/v1", }) void toString(String group, String version, String expected) { - final var result = ApiVersion.builder().group(group).version(version).build() + final String result = ApiVersion.builder().group(group).version(version).build() .toString(); assertEquals(expected, result); } diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/SchemaUtilsTest.java b/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/SchemaUtilsTest.java index 9b3a3d1eee6..67a7e3752f0 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/SchemaUtilsTest.java +++ b/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/SchemaUtilsTest.java @@ -31,8 +31,9 @@ import org.junit.jupiter.params.provider.CsvSource; import java.io.File; +import java.util.Arrays; import java.util.LinkedHashMap; -import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; @@ -56,7 +57,7 @@ void setUp(@TempDir File tempDir) { "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta, ListMeta" }) void refToClassName(String ref, String expected) { - final var result = SchemaUtils.refToClassName(ref); + final String result = SchemaUtils.refToClassName(ref); assertEquals(expected, result); } @@ -68,12 +69,12 @@ void refToClassName(String ref, String expected) { "io.k8s.apimachinery, io.k8s.apimachinery" }) void toModelPackage(String packageName, String expected) { - final var settings = generatorSettingsBuilder + final GeneratorSettings settings = generatorSettingsBuilder .packageMapping("io.k8s.api.core.v1", "io.fabric8.kubernetes.api.model") .packageMapping("io.k8s.api.core", "io.fabric8.kubernetes.api.model.core") .packageMapping("io.k8s.api.networking", "io.fabric8.kubernetes.api.model.networking") .build(); - final var result = new SchemaUtils(settings).toModelPackage(packageName); + final String result = new SchemaUtils(settings).toModelPackage(packageName); assertEquals(expected, result); } @@ -100,7 +101,7 @@ void sanitizeVariable(String variable, String expected) { "items, getItems" }) void getterName(String variable, String expected) { - final var result = SchemaUtils.getterName(variable); + final String result = SchemaUtils.getterName(variable); assertEquals(expected, result); } @@ -113,7 +114,7 @@ void getterName(String variable, String expected) { "items, setItems" }) void setterName(String variable, String expected) { - final var result = SchemaUtils.setterName(variable); + final String result = SchemaUtils.setterName(variable); assertEquals(expected, result); } @@ -146,42 +147,42 @@ void setUp() { @Test void arrays() { - final var schema = new ArraySchema(); + final ArraySchema schema = new ArraySchema(); schema.items(new StringSchema()); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("List", result); assertEquals("java.util.List", addImport.get()); } @Test void mapOfObjects() { - final var schema = new MapSchema(); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final MapSchema schema = new MapSchema(); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("Map", result); assertEquals("java.util.Map", addImport.get()); } @Test void mapOfIntegers() { - final var schema = new MapSchema(); + final MapSchema schema = new MapSchema(); schema.additionalProperties(new IntegerSchema()); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("Map", result); assertEquals("java.util.Map", addImport.get()); } @Test void date() { - final var schema = new DateSchema(); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final DateSchema schema = new DateSchema(); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("String", result); assertNull(addImport.get()); } @Test void dateTime() { - final var schema = new DateTimeSchema(); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final DateTimeSchema schema = new DateTimeSchema(); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("String", result); assertNull(addImport.get()); } @@ -191,78 +192,78 @@ void dateTime() { // Fabric8 has a special (legacy) handling for MicroTime, we might consider removing this // completely and use OffsetDateTime as for the rest of temporal types void microTime() { - final var schema = new ObjectSchema(); + final ObjectSchema schema = new ObjectSchema(); schema.set$ref("#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.MicroTime"); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("MicroTime", result); assertEquals("io.fabric8.kubernetes.api.model.MicroTime", addImport.get()); } @Test void string() { - final var schema = new StringSchema(); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final StringSchema schema = new StringSchema(); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("String", result); assertNull(addImport.get()); } @Test void integer() { - final var schema = new IntegerSchema(); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final IntegerSchema schema = new IntegerSchema(); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("Integer", result); assertNull(addImport.get()); } @Test void _long() { - final var schema = new NumberSchema(); + final NumberSchema schema = new NumberSchema(); schema.setFormat("int64"); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("Long", result); assertNull(addImport.get()); } @Test void _double() { - final var schema = new NumberSchema(); + final NumberSchema schema = new NumberSchema(); schema.setFormat("double"); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("Double", result); assertNull(addImport.get()); } @Test void ref() { - final var schema = new ObjectSchema(); + final ObjectSchema schema = new ObjectSchema(); schema.set$ref("#/definitions/io.k8s.api.core.v1.Pod"); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("Pod", result); assertEquals("io.fabric8.kubernetes.api.model.Pod", addImport.get()); } @Test void intOrString() { - final var schema = new ObjectSchema(); + final ObjectSchema schema = new ObjectSchema(); schema.set$ref("#/components/schemas/io.k8s.apimachinery.pkg.util.intstr.IntOrString"); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("IntOrString", result); assertEquals("io.fabric8.kubernetes.api.model.IntOrString", addImport.get()); } @Test void jsonNode() { - final var schema = new ObjectSchema(); + final ObjectSchema schema = new ObjectSchema(); schema.set$ref("#/components/schemas/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1.JSON"); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("JsonNode", result); assertEquals("com.fasterxml.jackson.databind.JsonNode", addImport.get()); } @Test void plainObject() { - final var schema = new ObjectSchema(); - final var result = schemaUtils.schemaToClassName(importConsumer, schema); + final ObjectSchema schema = new ObjectSchema(); + final String result = schemaUtils.schemaToClassName(importConsumer, schema); assertEquals("KubernetesResource", result); assertEquals("io.fabric8.kubernetes.api.model.KubernetesResource", addImport.get()); } @@ -274,14 +275,14 @@ class PropertyOrder { @Test void nullSchema() { - final var result = SchemaUtils.propertyOrder(null); + final Map result = SchemaUtils.propertyOrder(null); assertEquals(1, result.size()); assertEquals(true, result.get("isEmpty")); } @Test void emptySchema() { - final var result = SchemaUtils.propertyOrder(new ObjectSchema()); + final Map result = SchemaUtils.propertyOrder(new ObjectSchema()); assertEquals(1, result.size()); assertEquals(true, result.get("isEmpty")); } @@ -289,7 +290,7 @@ void emptySchema() { @Test void withProperties() { // Given - final var schema = new ObjectSchema(); + final ObjectSchema schema = new ObjectSchema(); schema.properties(new LinkedHashMap<>()); schema.getProperties().put("propZ", new StringSchema()); schema.getProperties().put("prop1", new StringSchema()); @@ -298,10 +299,10 @@ void withProperties() { schema.getProperties().put("items", new ArraySchema()); schema.getProperties().put("apiVersion", new StringSchema()); // When - final var result = SchemaUtils.propertyOrder(schema); + final Map result = SchemaUtils.propertyOrder(schema); // Then assertEquals(false, result.get("isEmpty")); - assertEquals(List.of("apiVersion", "kind", "metadata", "propZ", "prop1", "items"), result.get("properties")); + assertEquals(Arrays.asList("apiVersion", "kind", "metadata", "propZ", "prop1", "items"), result.get("properties")); } } } diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/model/TemplateContextTest.java b/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/model/TemplateContextTest.java index f6fcf9453d3..de1bb455baa 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/model/TemplateContextTest.java +++ b/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/model/TemplateContextTest.java @@ -22,9 +22,10 @@ import org.junit.jupiter.api.io.TempDir; import java.io.File; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.AbstractMap; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -46,8 +47,8 @@ void setUp(@TempDir File tempDir) { .packageMapping("io.k8s.apimachinery.pkg.apis.meta.v1", "io.fabric8.kubernetes.api.model") .build(); - final var schema = (Schema) settings.getOpenAPI().getComponents().getSchemas().get("io.k8s.api.core.v1.Pod"); - templateContext = new TemplateContext(settings, Map.entry("io.k8s.api.core.v1.Pod", schema)); + final Schema schema = (Schema) settings.getOpenAPI().getComponents().getSchemas().get("io.k8s.api.core.v1.Pod"); + templateContext = new TemplateContext(settings, new AbstractMap.SimpleEntry<>("io.k8s.api.core.v1.Pod", schema)); } @Test @@ -101,7 +102,7 @@ void kubernetesListType() { @Test void context() { assertEquals(1, templateContext.getContext().size()); - assertEquals(Set.of("imports"), templateContext.getContext().keySet()); + assertEquals(new HashSet<>(Collections.singletonList("imports")), templateContext.getContext().keySet()); } @Test @@ -113,13 +114,13 @@ void put() { @Test void addImport() { templateContext.addImport("java.util.List"); - assertEquals(Set.of("java.util.List"), templateContext.getContext().get("imports")); + assertEquals(new HashSet<>(Collections.singletonList("java.util.List")), templateContext.getContext().get("imports")); } @Test void addAllImports() { - templateContext.addAllImports(List.of("java.util.List", "java.util.Map")); - assertEquals(Set.of("java.util.List", "java.util.Map"), templateContext.getContext().get("imports")); + templateContext.addAllImports(Arrays.asList("java.util.List", "java.util.Map")); + assertEquals(new HashSet<>(Arrays.asList("java.util.List", "java.util.Map")), templateContext.getContext().get("imports")); } }