Skip to content

Commit

Permalink
refactor: reorganized schema utils
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa authored Jul 26, 2024
1 parent 1acdcb7 commit 2090acd
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,34 @@
// Ported from https://github.com/manusa/yakc/blob/9272d649bfe05cd536d417fec64dcf679877bd14/buildSrc/src/main/java/com/marcnuri/yakc/schema/GeneratorSettings.java
package io.fabric8.kubernetes.schema.generator;

import io.swagger.v3.oas.models.Components;
import io.fabric8.kubernetes.schema.generator.schema.SchemaUtils;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.SpecVersion;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.parser.OpenAPIV3Parser;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Singular;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static io.fabric8.kubernetes.schema.generator.SchemaUtils.APPLICATION_JSON;
import static io.fabric8.kubernetes.schema.generator.schema.SchemaUtils.APPLICATION_JSON;
import static io.fabric8.kubernetes.schema.generator.schema.SchemaUtils.mergeSchemas;

@Builder(toBuilder = true)
@NoArgsConstructor
Expand Down Expand Up @@ -113,7 +111,9 @@ public class GeneratorSettings {
public OpenAPI getOpenAPI() {
synchronized (this) {
if (openAPI == null) {
openAPI = mergeSchemas(schemas);
final List<OpenAPI> openApis = new ArrayList<>();
schemas.stream().map(SchemaUtils::parse).forEach(openApis::add);
openAPI = mergeSchemas(openApis);
}
}
return openAPI;
Expand Down Expand Up @@ -258,39 +258,4 @@ private static List<Parameter> pathParameters(Operation operation) {
.collect(Collectors.toList());
}

private static OpenAPI mergeSchemas(List<File> schemas) {
final OpenAPI openAPI = new OpenAPI(SpecVersion.V31);
openAPI.setComponents(new Components());
openAPI.getComponents().setSchemas(new HashMap<>());
openAPI.getComponents().setResponses(new HashMap<>());
openAPI.getComponents().setParameters(new HashMap<>());
openAPI.getComponents().setExamples(new HashMap<>());
openAPI.getComponents().setRequestBodies(new HashMap<>());
openAPI.getComponents().setHeaders(new HashMap<>());
openAPI.getComponents().setSecuritySchemes(new HashMap<>());
openAPI.getComponents().setLinks(new HashMap<>());
openAPI.getComponents().setCallbacks(new HashMap<>());
openAPI.getComponents().setExtensions(new HashMap<>());
openAPI.setPaths(new Paths());
for (File schema : schemas) {
if (schema == null || !schema.exists()) {
throw new IllegalArgumentException("Schema file not found: " + schema);
}
final OpenAPI currentApi = new OpenAPIV3Parser().read(schema.getAbsolutePath());
new InlineModelResolver().flatten(currentApi);
openAPI.getPaths().putAll(currentApi.getPaths());
if (currentApi.getComponents() != null) {
Optional.ofNullable(currentApi.getComponents().getSchemas()).ifPresent(openAPI.getComponents().getSchemas()::putAll);
Optional.ofNullable(currentApi.getComponents().getResponses())
.ifPresent(openAPI.getComponents().getResponses()::putAll);
Optional.ofNullable(currentApi.getComponents().getParameters())
.ifPresent(openAPI.getComponents().getParameters()::putAll);
Optional.ofNullable(currentApi.getComponents().getExamples()).ifPresent(openAPI.getComponents().getExamples()::putAll);
Optional.ofNullable(currentApi.getComponents().getRequestBodies())
.ifPresent(openAPI.getComponents().getRequestBodies()::putAll);
Optional.ofNullable(currentApi.getComponents().getHeaders()).ifPresent(openAPI.getComponents().getHeaders()::putAll);
}
}
return openAPI;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// Ported from https://github.com/manusa/yakc/blob/9272d649bfe05cd536d417fec64dcf679877bd14/buildSrc/src/main/java/com/marcnuri/yakc/schema/GeneratorUtils.java
package io.fabric8.kubernetes.schema.generator;

import io.fabric8.kubernetes.schema.generator.schema.SchemaUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.fabric8.kubernetes.schema.generator.GeneratorException;
import io.fabric8.kubernetes.schema.generator.GeneratorSettings;
import io.fabric8.kubernetes.schema.generator.GeneratorUtils;
import io.fabric8.kubernetes.schema.generator.SchemaUtils;
import io.fabric8.kubernetes.schema.generator.schema.SchemaUtils;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.io.FileUtils;
Expand All @@ -46,15 +46,15 @@
import java.util.stream.Collectors;

import static io.fabric8.kubernetes.schema.generator.GeneratorUtils.cleanSourceDirectory;
import static io.fabric8.kubernetes.schema.generator.SchemaUtils.deserializerForJavaClass;
import static io.fabric8.kubernetes.schema.generator.SchemaUtils.getterName;
import static io.fabric8.kubernetes.schema.generator.SchemaUtils.isArray;
import static io.fabric8.kubernetes.schema.generator.SchemaUtils.isMap;
import static io.fabric8.kubernetes.schema.generator.SchemaUtils.sanitizeDescription;
import static io.fabric8.kubernetes.schema.generator.SchemaUtils.sanitizeVariable;
import static io.fabric8.kubernetes.schema.generator.SchemaUtils.serializerForJavaClass;
import static io.fabric8.kubernetes.schema.generator.SchemaUtils.serializerForSchema;
import static io.fabric8.kubernetes.schema.generator.SchemaUtils.setterName;
import static io.fabric8.kubernetes.schema.generator.schema.SchemaUtils.deserializerForJavaClass;
import static io.fabric8.kubernetes.schema.generator.schema.SchemaUtils.getterName;
import static io.fabric8.kubernetes.schema.generator.schema.SchemaUtils.isArray;
import static io.fabric8.kubernetes.schema.generator.schema.SchemaUtils.isMap;
import static io.fabric8.kubernetes.schema.generator.schema.SchemaUtils.sanitizeDescription;
import static io.fabric8.kubernetes.schema.generator.schema.SchemaUtils.sanitizeVariable;
import static io.fabric8.kubernetes.schema.generator.schema.SchemaUtils.serializerForJavaClass;
import static io.fabric8.kubernetes.schema.generator.schema.SchemaUtils.serializerForSchema;
import static io.fabric8.kubernetes.schema.generator.schema.SchemaUtils.setterName;

class ModelGenerator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.fabric8.kubernetes.schema.generator.ApiVersion;
import io.fabric8.kubernetes.schema.generator.GeneratorSettings;
import io.fabric8.kubernetes.schema.generator.ImportOrderComparator;
import io.fabric8.kubernetes.schema.generator.SchemaUtils;
import io.fabric8.kubernetes.schema.generator.schema.SchemaUtils;
import io.swagger.v3.oas.models.media.Schema;
import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.schema.generator;
package io.fabric8.kubernetes.schema.generator.schema;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,25 @@
* limitations under the License.
*/
// Ported from https://github.com/manusa/yakc/blob/9272d649bfe05cd536d417fec64dcf679877bd14/buildSrc/src/main/java/com/marcnuri/yakc/schema/SchemaUtils.java
package io.fabric8.kubernetes.schema.generator;

package io.fabric8.kubernetes.schema.generator.schema;

import io.fabric8.kubernetes.schema.generator.GeneratorSettings;
import io.fabric8.kubernetes.schema.generator.PropertyOrderComparator;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.SpecVersion;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.DateTimeSchema;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.parser.OpenAPIV3Parser;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -103,9 +112,9 @@ public class SchemaUtils {
}

private static final Map<String, String> 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");
}
// 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<String, String> TYPE_MAP = new LinkedHashMap<>();
static {
Expand Down Expand Up @@ -380,6 +389,46 @@ public static Map<String, Object> propertyOrder(Schema<?> schema) {
return ret;
}

public static OpenAPI parse(File schema) {
if (schema == null || !schema.exists()) {
throw new IllegalArgumentException("Schema file not found: " + schema);
}
final OpenAPI openApi = new OpenAPIV3Parser().read(schema.getAbsolutePath());
new InlineModelResolver().flatten(openApi);
return openApi;
}

public static OpenAPI mergeSchemas(Collection<OpenAPI> schemas) {
final OpenAPI openAPI = new OpenAPI(SpecVersion.V31);
openAPI.setComponents(new Components());
openAPI.getComponents().setSchemas(new HashMap<>());
openAPI.getComponents().setResponses(new HashMap<>());
openAPI.getComponents().setParameters(new HashMap<>());
openAPI.getComponents().setExamples(new HashMap<>());
openAPI.getComponents().setRequestBodies(new HashMap<>());
openAPI.getComponents().setHeaders(new HashMap<>());
openAPI.getComponents().setSecuritySchemes(new HashMap<>());
openAPI.getComponents().setLinks(new HashMap<>());
openAPI.getComponents().setCallbacks(new HashMap<>());
openAPI.getComponents().setExtensions(new HashMap<>());
openAPI.setPaths(new Paths());
for (OpenAPI currentApi : schemas) {
openAPI.getPaths().putAll(currentApi.getPaths());
if (currentApi.getComponents() != null) {
Optional.ofNullable(currentApi.getComponents().getSchemas()).ifPresent(openAPI.getComponents().getSchemas()::putAll);
Optional.ofNullable(currentApi.getComponents().getResponses())
.ifPresent(openAPI.getComponents().getResponses()::putAll);
Optional.ofNullable(currentApi.getComponents().getParameters())
.ifPresent(openAPI.getComponents().getParameters()::putAll);
Optional.ofNullable(currentApi.getComponents().getExamples()).ifPresent(openAPI.getComponents().getExamples()::putAll);
Optional.ofNullable(currentApi.getComponents().getRequestBodies())
.ifPresent(openAPI.getComponents().getRequestBodies()::putAll);
Optional.ofNullable(currentApi.getComponents().getHeaders()).ifPresent(openAPI.getComponents().getHeaders()::putAll);
}
}
return openAPI;
}

public static String capitalize(String string) {
if (string == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.kubernetes.schema.generator;

import io.fabric8.kubernetes.schema.generator.schema.SchemaUtils;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.DateSchema;
import io.swagger.v3.oas.models.media.DateTimeSchema;
Expand Down

0 comments on commit 2090acd

Please sign in to comment.