diff --git a/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/AbstractJsonSchema.java b/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/AbstractJsonSchema.java index 0b82b7c98dc..d1229bdace5 100644 --- a/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/AbstractJsonSchema.java +++ b/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/AbstractJsonSchema.java @@ -40,6 +40,8 @@ import io.fabric8.crd.generator.annotation.SelectableField; import io.fabric8.crdv2.generator.InternalSchemaSwaps.SwapResult; import io.fabric8.crdv2.generator.ResolvingContext.GeneratorObjectSchema; +import io.fabric8.crdv2.generator.v1.JsonSchema.V1JSONSchemaProps; +import io.fabric8.crdv2.generator.v1.SchemaCustomizer; import io.fabric8.generator.annotation.Default; import io.fabric8.generator.annotation.Max; import io.fabric8.generator.annotation.Min; @@ -53,6 +55,7 @@ import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.IntOrString; import io.fabric8.kubernetes.api.model.Quantity; +import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps; import io.fabric8.kubernetes.api.model.runtime.RawExtension; import io.fabric8.kubernetes.client.utils.Utils; import io.fabric8.kubernetes.model.annotation.LabelSelector; @@ -411,7 +414,7 @@ private T resolveObject(LinkedHashMap visited, InternalSchemaSwa String... ignore) { Set ignores = ignore.length > 0 ? new LinkedHashSet<>(Arrays.asList(ignore)) : Collections.emptySet(); - final T objectSchema = singleProperty("object"); + T objectSchema = singleProperty("object"); schemaSwaps = schemaSwaps.branchAnnotations(); final InternalSchemaSwaps swaps = schemaSwaps; @@ -500,6 +503,25 @@ private T resolveObject(LinkedHashMap visited, InternalSchemaSwa consumeRepeatingAnnotation(rawClass, ValidationRule.class, v -> validationRules.add(from(v))); addToValidationRules(objectSchema, validationRules); + + if (objectSchema instanceof JSONSchemaProps) { + JSONSchemaProps[] props = new JSONSchemaProps[] {(JSONSchemaProps) objectSchema}; + consumeRepeatingAnnotation(rawClass, SchemaCustomizer.class, sc -> { + try { + props[0] = sc.value().getConstructor().newInstance().apply(props[0], sc.input()); + } catch (Exception e) { + if (!(e instanceof RuntimeException)) { + e = new RuntimeException(e); + } + throw (RuntimeException)e; + } + }); + if (props[0] != objectSchema) { + // hack to convert back to V1JSONSchemaProps + objectSchema = (T) resolvingContext.kubernetesSerialization.convertValue(props[0], V1JSONSchemaProps.class); + } + } + return objectSchema; } diff --git a/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/v1/SchemaCustomizer.java b/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/v1/SchemaCustomizer.java new file mode 100644 index 00000000000..7726f73e245 --- /dev/null +++ b/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/v1/SchemaCustomizer.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.crdv2.generator.v1; + +import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.function.BiFunction; + +@Target({ ElementType.TYPE }) +@Retention(RetentionPolicy.RUNTIME) +public @interface SchemaCustomizer { + + Class> value(); + + String input(); + +}