Skip to content

Commit

Permalink
Fix #3450: Cast to String for specific annotations in CRD Generator
Browse files Browse the repository at this point in the history
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
  • Loading branch information
deepsan authored and manusa committed Sep 8, 2021
1 parent 1e8067a commit e1a1b3b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void shouldAugmentPropertiesSchemaFromAnnotations() {
assertEquals(2, properties.size());
final JSONSchemaProps specSchema = properties.get("spec");
Map<String, JSONSchemaProps> spec = specSchema.getProperties();
assertEquals(5, spec.size());
assertEquals(6, spec.size());

// check descriptions are present
assertTrue(spec.containsKey("from-field"));
Expand Down

0 comments on commit e1a1b3b

Please sign in to comment.