From e1a1b3b2c9e6c4f0ce512b4fa7e1b362ade29f61 Mon Sep 17 00:00:00 2001 From: deepsan Date: Tue, 7 Sep 2021 16:57:32 -0700 Subject: [PATCH] Fix #3450: Cast to String for specific annotations in CRD Generator When looping through annotated fields/methods, the current code assumes a `String` type `value` attribute. This fails with a `ClassCastException` if any annotation uses a different type. `JsonSchemaTest` fails with a ``` java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String ``` error without the fix; passes with the fix. Fixes #3450 --- CHANGELOG.md | 1 + .../io/fabric8/crd/generator/AbstractJsonSchema.java | 11 ++++++----- .../fabric8/crd/example/annotated/AnnotatedSpec.java | 3 +++ .../io/fabric8/crd/generator/v1/JsonSchemaTest.java | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b0f90f8ca5..e669be8a800 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ #### Bugs * Fix: Extension annotator doesn't generate XxxEditable classes +* Fix #3450: CRD generator fails with ClassCastException in some cases #### Dependency Upgrade * Fix #3438: Upgrade Sundrio to 0.50.1 diff --git a/crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java b/crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java index 78c0654f9e6..de7ac69cf27 100644 --- a/crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java +++ b/crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java @@ -190,19 +190,20 @@ static PropertyOrAccessor fromMethod(Method method, String propertyName) { public void process() { annotations.forEach(a -> { - final String fromAnnotation = (String) a.getParameters().get("value"); switch (a.getClassRef().getFullyQualifiedName()) { case ANNOTATION_NOT_NULL: required = true; break; case ANNOTATION_JSON_PROPERTY: - if (!Strings.isNullOrEmpty(fromAnnotation) && !propertyName.equals(fromAnnotation)) { - renamedTo = fromAnnotation; + final String nameFromAnnotation = (String) a.getParameters().get("value"); + if (!Strings.isNullOrEmpty(nameFromAnnotation) && !propertyName.equals(nameFromAnnotation)) { + renamedTo = nameFromAnnotation; } break; case ANNOTATION_JSON_PROPERTY_DESCRIPTION: - if (!Strings.isNullOrEmpty(fromAnnotation)) { - description = fromAnnotation; + final String descriptionFromAnnotation = (String) a.getParameters().get("value"); + if (!Strings.isNullOrEmpty(descriptionFromAnnotation)) { + description = descriptionFromAnnotation; } break; } diff --git a/crd-generator/api/src/test/java/io/fabric8/crd/example/annotated/AnnotatedSpec.java b/crd-generator/api/src/test/java/io/fabric8/crd/example/annotated/AnnotatedSpec.java index ac75a936701..89d9f8d5a50 100644 --- a/crd-generator/api/src/test/java/io/fabric8/crd/example/annotated/AnnotatedSpec.java +++ b/crd-generator/api/src/test/java/io/fabric8/crd/example/annotated/AnnotatedSpec.java @@ -18,6 +18,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyDescription; +import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; public class AnnotatedSpec { @@ -30,6 +31,8 @@ public class AnnotatedSpec { @NotNull private boolean emptySetter; private AnnotatedEnum anEnum; + @Min(0) // a non-string value attribute + private int sizedField; @JsonProperty("from-getter") @JsonPropertyDescription("from-getter-description") diff --git a/crd-generator/api/src/test/java/io/fabric8/crd/generator/v1/JsonSchemaTest.java b/crd-generator/api/src/test/java/io/fabric8/crd/generator/v1/JsonSchemaTest.java index a28ab063e75..6fad8e3dcbc 100644 --- a/crd-generator/api/src/test/java/io/fabric8/crd/generator/v1/JsonSchemaTest.java +++ b/crd-generator/api/src/test/java/io/fabric8/crd/generator/v1/JsonSchemaTest.java @@ -75,7 +75,7 @@ void shouldAugmentPropertiesSchemaFromAnnotations() { assertEquals(2, properties.size()); final JSONSchemaProps specSchema = properties.get("spec"); Map spec = specSchema.getProperties(); - assertEquals(5, spec.size()); + assertEquals(6, spec.size()); // check descriptions are present assertTrue(spec.containsKey("from-field"));