Skip to content

Commit

Permalink
fix property case-handling during CRD gen (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
euberseder-hubspot authored Jan 27, 2025
1 parent 5f25722 commit ae1243d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
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)) {
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() {
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"));
}
}

0 comments on commit ae1243d

Please sign in to comment.