Skip to content

Commit 634daa1

Browse files
committed
fix: add a back door to modify the schema
closes: #7355 Signed-off-by: Steve Hawkins <[email protected]>
1 parent 04f7f5e commit 634daa1

File tree

5 files changed

+128
-1
lines changed

5 files changed

+128
-1
lines changed

crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/AbstractJsonSchema.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import io.fabric8.crd.generator.annotation.SelectableField;
4242
import io.fabric8.crdv2.generator.InternalSchemaSwaps.SwapResult;
4343
import io.fabric8.crdv2.generator.ResolvingContext.GeneratorObjectSchema;
44+
import io.fabric8.crdv2.generator.v1.JsonSchema.V1JSONSchemaProps;
45+
import io.fabric8.crdv2.generator.v1.SchemaCustomizer;
4446
import io.fabric8.generator.annotation.Default;
4547
import io.fabric8.generator.annotation.Max;
4648
import io.fabric8.generator.annotation.Min;
@@ -54,6 +56,7 @@
5456
import io.fabric8.kubernetes.api.model.HasMetadata;
5557
import io.fabric8.kubernetes.api.model.IntOrString;
5658
import io.fabric8.kubernetes.api.model.Quantity;
59+
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
5760
import io.fabric8.kubernetes.api.model.runtime.RawExtension;
5861
import io.fabric8.kubernetes.client.utils.Utils;
5962
import io.fabric8.kubernetes.model.annotation.LabelSelector;
@@ -412,7 +415,7 @@ private T resolveObject(LinkedHashMap<String, String> visited, InternalSchemaSwa
412415
String... ignore) {
413416
Set<String> ignores = ignore.length > 0 ? new LinkedHashSet<>(Arrays.asList(ignore)) : Collections.emptySet();
414417

415-
final T objectSchema = singleProperty("object");
418+
T objectSchema = singleProperty("object");
416419

417420
schemaSwaps = schemaSwaps.branchAnnotations();
418421
final InternalSchemaSwaps swaps = schemaSwaps;
@@ -505,6 +508,27 @@ private T resolveObject(LinkedHashMap<String, String> visited, InternalSchemaSwa
505508
consumeRepeatingAnnotation(rawClass, ValidationRule.class,
506509
v -> validationRules.add(from(v)));
507510
addToValidationRules(objectSchema, validationRules);
511+
return handleSchemaCustomizer(objectSchema, rawClass);
512+
}
513+
514+
private T handleSchemaCustomizer(T objectSchema, Class<?> rawClass) {
515+
if (objectSchema instanceof JSONSchemaProps) {
516+
JSONSchemaProps[] props = new JSONSchemaProps[] { (JSONSchemaProps) objectSchema };
517+
consumeRepeatingAnnotation(rawClass, SchemaCustomizer.class, sc -> {
518+
try {
519+
props[0] = sc.value().getConstructor().newInstance().apply(props[0], sc.input());
520+
} catch (Exception e) {
521+
if (!(e instanceof RuntimeException)) {
522+
e = new RuntimeException(e);
523+
}
524+
throw (RuntimeException) e;
525+
}
526+
});
527+
if (props[0] != objectSchema) {
528+
// hack to convert back to V1JSONSchemaProps
529+
objectSchema = (T) resolvingContext.kubernetesSerialization.convertValue(props[0], V1JSONSchemaProps.class);
530+
}
531+
}
508532
return objectSchema;
509533
}
510534

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.crdv2.generator.v1;
17+
18+
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
19+
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
@Target({ ElementType.TYPE })
26+
@Retention(RetentionPolicy.RUNTIME)
27+
public @interface SchemaCustomizer {
28+
29+
public interface Customizer {
30+
31+
/**
32+
* Customizes the given {@link JSONSchemaProps}
33+
*
34+
* @param props the {@link JSONSchemaProps} to customize
35+
* @param input the input String from the {@link SchemaCustomizer} annotation
36+
* @return the customized {@link JSONSchemaProps}
37+
*/
38+
JSONSchemaProps apply(JSONSchemaProps props, String input);
39+
40+
}
41+
42+
Class<? extends Customizer> value();
43+
44+
String input() default "";
45+
46+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.fabric8.crdv2.example.customized;
2+
3+
import io.fabric8.crdv2.generator.v1.SchemaCustomizer;
4+
5+
@SchemaCustomizer(input = "my description", value = DescriptionCustomizer.class)
6+
public class Customized {
7+
8+
public int field;
9+
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.fabric8.crdv2.example.customized;
2+
3+
import io.fabric8.crdv2.generator.v1.SchemaCustomizer;
4+
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
5+
6+
public class DescriptionCustomizer implements SchemaCustomizer.Customizer {
7+
8+
@Override
9+
public JSONSchemaProps apply(JSONSchemaProps props, String input) {
10+
props.setDescription(input);
11+
return props;
12+
}
13+
14+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.crdv2.generator.v1;
17+
18+
import io.fabric8.crdv2.example.customized.Customized;
19+
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
24+
class SchemaCustomizerTest {
25+
26+
@Test
27+
void applyCustomizer() {
28+
JSONSchemaProps schema = JsonSchema.from(Customized.class);
29+
assertEquals("my description", schema.getDescription());
30+
31+
}
32+
33+
}

0 commit comments

Comments
 (0)