Skip to content

Commit

Permalink
fix fabric8io#5262: making all collections as omit empty
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Jul 9, 2023
1 parent b81ebae commit a70b0ed
Showing 1 changed file with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jsonschema2pojo.rules.RuleFactory;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -46,16 +47,23 @@ public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVa
JType fieldType = field.type();
String fieldTypeName = fieldType.fullName();

if (ruleFactory.getGenerationConfig().isInitializeCollections() && fieldTypeName.startsWith(Map.class.getName())
&& (node == null || node.asText() == null || node.asText().isEmpty())) {
JClass keyGenericType = ((JClass) fieldType).getTypeParameters().get(0);
JClass valueGenericType = ((JClass) fieldType).getTypeParameters().get(1);
if (ruleFactory.getGenerationConfig().isInitializeCollections()) {
// add a default for maps if missing, rejected upstream https://github.com/joelittlejohn/jsonschema2pojo/issues/955
if (fieldTypeName.startsWith(Map.class.getName()) && (node == null || node.asText() == null || node.asText().isEmpty())) {
JClass keyGenericType = ((JClass) fieldType).getTypeParameters().get(0);
JClass valueGenericType = ((JClass) fieldType).getTypeParameters().get(1);

JClass mapImplClass = fieldType.owner().ref(LinkedHashMap.class);
mapImplClass = mapImplClass.narrow(keyGenericType, valueGenericType);
// maps are not marked as omitJavaEmpty - it's simplest to just add the annotation here, rather than updating the generator
field.annotate(JsonInclude.class).param(KubernetesCoreTypeAnnotator.ANNOTATION_VALUE, JsonInclude.Include.NON_EMPTY);
field.init(JExpr._new(mapImplClass));
JClass mapImplClass = fieldType.owner().ref(LinkedHashMap.class);
mapImplClass = mapImplClass.narrow(keyGenericType, valueGenericType);
field.init(JExpr._new(mapImplClass));
}

// maps and some lists are not marked as omitJavaEmpty - it's simplest to just add the annotation here, rather than updating the generator
JClass jsonInclude = fieldType.owner().ref(JsonInclude.class);
if ((fieldTypeName.startsWith(Map.class.getName()) || fieldTypeName.startsWith(List.class.getName()))
&& field.annotations().stream().noneMatch(annotation -> annotation.getAnnotationClass().isAssignableFrom(jsonInclude))) {
field.annotate(jsonInclude).param(KubernetesCoreTypeAnnotator.ANNOTATION_VALUE, JsonInclude.Include.NON_EMPTY);
}
}

return super.apply(nodeName, node, parent, field, currentSchema);
Expand Down

0 comments on commit a70b0ed

Please sign in to comment.