Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix property case-handling during CRD gen #194

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ public void process() {
break;
case ANNOTATION_JSON_PROPERTY:
final String nameFromAnnotation = (String) a.getParameters().get(VALUE);
if (!Strings.isNullOrEmpty(nameFromAnnotation) && !propertyName.equals(nameFromAnnotation)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To test my understanding - this change is necessary because we want the nameFromAnnotation to override anything we might do from getCaseCorrectedPropertyName() ? Since if renamedTo is not null, the result of getCaseCorrectedPropertyName() is unused.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. If someone actually wanted getHBaseImage() to generate a CRD field of hBaseImage instead of hbaseImage, then they should use JsonProperty to annotate that getter to override our default conversion, but if the internal field name was already hBaseImage (because maybe the field is generated with Immutables) then without this line setting that wouldn't be detected as an override and we'd use the new convention anyway.

if (!Strings.isNullOrEmpty(nameFromAnnotation)) {
renamedTo = nameFromAnnotation;
}
break;
Expand Down Expand Up @@ -711,12 +711,38 @@ public Property process() {
}
});

String caseCorrectedPropertyName = getCaseCorrectedPropertyName();
if(renamedTo == null && !original.getName().equals(caseCorrectedPropertyName) ) {
renamedTo = caseCorrectedPropertyName;
}

TypeRef typeRef = schemaFrom != null ? schemaFrom : parameterMap.exchange(original.getTypeRef());
String finalName = renamedTo != null ? renamedTo : original.getName();

return new Property(original.getAnnotations(), typeRef, finalName,
original.getComments(), false, false, original.getModifiers(), original.getAttributes());
}

private String getCaseCorrectedPropertyName() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment here describing what "case correction" in this case means?

String capitalizedOriginalPropertyName = original.getNameCapitalized();
StringBuilder newPropertyNameBuilder = new StringBuilder();

int index;
for (index = 0;index<capitalizedOriginalPropertyName.length();index++) {
char charAtIndex = capitalizedOriginalPropertyName.charAt(index);
if(Character.isUpperCase(charAtIndex)) {
newPropertyNameBuilder.append(Character.toLowerCase(charAtIndex));
} else {
break;
}
}

if(index<capitalizedOriginalPropertyName.length()) {
newPropertyNameBuilder.append(capitalizedOriginalPropertyName.substring(index));
}

return newPropertyNameBuilder.toString();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.fabric8.crd.example.serialization;

import com.fasterxml.jackson.annotation.JsonProperty;

public class AnnotatedSerializationExample {
String fOoBar;

@JsonProperty("fOoBar")
public String getFOoBar() {
return fOoBar;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.fabric8.crd.example.serialization;

public class SerializationExample {
String uRL;
String fOoBar;
String normalCase;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import io.fabric8.crd.example.generic.ResourceWithGeneric;
import io.fabric8.crd.example.json.ContainingJson;
import io.fabric8.crd.example.person.Person;
import io.fabric8.crd.example.serialization.AnnotatedSerializationExample;
import io.fabric8.crd.example.serialization.SerializationExample;
import io.fabric8.crd.generator.utils.Types;
import io.fabric8.kubernetes.api.model.AnyType;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
Expand Down Expand Up @@ -445,4 +447,30 @@ void shouldProcessGenericClasses() {
assertNotNull(corgeItemsProps);
assertEquals("string", corgeItemsProps.getType());
}

@Test
void shouldProduceDeserializableNames() {
TypeDef serializationExample = Types.typeDefFrom(SerializationExample.class);
JSONSchemaProps schema = JsonSchema.from(serializationExample);
assertNotNull(schema);

Map<String, JSONSchemaProps> properties = schema.getProperties();
assertEquals(3, properties.size());

assertTrue(properties.containsKey("url"));
assertTrue(properties.containsKey("fooBar"));
assertTrue(properties.containsKey("normalCase"));
}

@Test
void itShouldNotChangeAnnotatedNames() {
TypeDef annotatedSerializationExample = Types.typeDefFrom(AnnotatedSerializationExample.class);
JSONSchemaProps schema = JsonSchema.from(annotatedSerializationExample);
assertNotNull(schema);

Map<String, JSONSchemaProps> properties = schema.getProperties();
assertEquals(1, properties.size());

assertTrue(properties.containsKey("fOoBar"));
}
}