Skip to content

Commit

Permalink
fix(crd-generator): fallback value of Default annotation in presenc…
Browse files Browse the repository at this point in the history
…e of multiple accessors (#5503)

* Attempt to reproduce 5501

* fix, fallback to previous value instead of null

* changelog

* fmt

* fix
  • Loading branch information
andreaTP authored Oct 16, 2023
1 parent 67244ab commit 77a65f7
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 6.10-SNAPSHOT

#### Bugs
* Fix #5501: [crd-generator] Fix fallback value of `Default` annotation in presence of multiple accessors

#### Improvements

Expand Down
5 changes: 5 additions & 0 deletions crd-generator/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
<artifactId>validation-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ public Property process() {
LOGGER.debug("Description for property {} has already been contributed by: {}", name, descriptionContributedBy);
}
}
defaultValue = p.getDefault().orElse(null);
defaultValue = p.getDefault().orElse(defaultValue);
min = p.getMin().orElse(min);
max = p.getMax().orElse(max);
pattern = p.getPattern().orElse(pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import io.fabric8.generator.annotation.Nullable;
import io.fabric8.generator.annotation.Pattern;
import io.fabric8.generator.annotation.Required;
import lombok.Data;

@Data
public class AnnotatedSpec {
@JsonProperty("from-field")
@JsonPropertyDescription("from-field-description")
Expand All @@ -37,6 +39,8 @@ public class AnnotatedSpec {
private String singleDigit;
private String nullable;
private String defaultValue;
@Default("my-value2")
private String defaultValue2;
@Required
private boolean emptySetter;
@Required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void shouldAugmentPropertiesSchemaFromAnnotations() throws JsonProcessingExcepti
assertNotNull(schema);
Map<String, JSONSchemaProps> properties = assertSchemaHasNumberOfProperties(schema, 2);
final JSONSchemaProps specSchema = properties.get("spec");
Map<String, JSONSchemaProps> spec = assertSchemaHasNumberOfProperties(specSchema, 12);
Map<String, JSONSchemaProps> spec = assertSchemaHasNumberOfProperties(specSchema, 13);

// check descriptions are present
assertTrue(spec.containsKey("from-field"));
Expand Down Expand Up @@ -154,6 +154,13 @@ void shouldAugmentPropertiesSchemaFromAnnotations() throws JsonProcessingExcepti
assertNull(defaultValue.getMaximum());
assertNull(defaultValue.getPattern());

final JSONSchemaProps defaultValue2 = spec.get("defaultValue2");
assertEquals("my-value2", YAML_MAPPER.writeValueAsString(defaultValue2.getDefault()).trim());
assertNull(defaultValue2.getNullable());
assertNull(defaultValue2.getMinimum());
assertNull(defaultValue2.getMaximum());
assertNull(defaultValue2.getPattern());

// check required list, should register properties with their modified name if needed
final List<String> required = specSchema.getRequired();
assertEquals(3, required.size());
Expand Down
6 changes: 6 additions & 0 deletions crd-generator/test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
<groupId>io.fabric8</groupId>
<artifactId>crd-generator-apt</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void testCrdv1beta1() {
assertEquals(1, v.getAdditionalPrinterColumns().get(0).getPriority());
assertEquals("UPTIME", v.getAdditionalPrinterColumns().get(1).getName());
assertEquals(0, v.getAdditionalPrinterColumns().get(1).getPriority());
assertEquals("false", props.getProperties().get("ephemeral").getDefault().asText());
});

Optional<CustomResourceDefinitionVersion> v1alpha1 = d.getSpec().getVersions().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
*/
package io.fabric8.crd.generator.zookeeper.v1;

import io.fabric8.generator.annotation.Default;
import io.fabric8.generator.annotation.Required;
import io.fabric8.kubernetes.model.annotation.SpecReplicas;
import lombok.Data;

@Data
public class ZookeeperSpec {

@SpecReplicas
private int size;
@Required
private String version;
@Default("false")
private boolean ephemeral;
}

0 comments on commit 77a65f7

Please sign in to comment.