From b62b8446c236186aa8bb00e5693a2024616e899f Mon Sep 17 00:00:00 2001 From: Steve Hawkins Date: Wed, 18 May 2022 09:24:31 -0400 Subject: [PATCH] fix #3625: adding defaults for maps --- .../jsonschema2pojo/Fabric8DefaultRule.java | 65 +++++++++++++++++++ .../jsonschema2pojo/Fabric8RuleFactory.java | 11 +++- .../pkg/schemagen/generate.go | 1 + 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8DefaultRule.java diff --git a/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8DefaultRule.java b/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8DefaultRule.java new file mode 100644 index 00000000000..b4137e804af --- /dev/null +++ b/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8DefaultRule.java @@ -0,0 +1,65 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fabric8.kubernetes.jsonschema2pojo; + +import com.fasterxml.jackson.databind.JsonNode; +import com.sun.codemodel.JClass; +import com.sun.codemodel.JExpr; +import com.sun.codemodel.JFieldVar; +import com.sun.codemodel.JType; +import org.jsonschema2pojo.Schema; +import org.jsonschema2pojo.rules.DefaultRule; +import org.jsonschema2pojo.rules.RuleFactory; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Adds default map handling + */ +public class Fabric8DefaultRule extends DefaultRule { + + private final RuleFactory ruleFactory; + + public Fabric8DefaultRule(RuleFactory ruleFactory) { + super(ruleFactory); + this.ruleFactory = ruleFactory; + } + + @Override + public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) { + + if (ruleFactory.getGenerationConfig().isInitializeCollections()) { + JType fieldType = field.type(); + String fieldTypeName = fieldType.fullName(); + + // check for a map with no defaults + 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); + + field.init(JExpr._new(mapImplClass)); + } + + } + return super.apply(nodeName, node, parent, field, currentSchema); + } + +} diff --git a/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8RuleFactory.java b/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8RuleFactory.java index 7daf1a8f499..dbd01248619 100644 --- a/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8RuleFactory.java +++ b/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8RuleFactory.java @@ -15,6 +15,9 @@ */ package io.fabric8.kubernetes.jsonschema2pojo; +import com.sun.codemodel.JFieldVar; +import com.sun.codemodel.JPackage; +import com.sun.codemodel.JType; import org.jsonschema2pojo.DefaultGenerationConfig; import org.jsonschema2pojo.Jackson2Annotator; import org.jsonschema2pojo.SchemaStore; @@ -23,9 +26,6 @@ import org.jsonschema2pojo.util.NameHelper; import org.jsonschema2pojo.util.ParcelableHelper; -import com.sun.codemodel.JPackage; -import com.sun.codemodel.JType; - public class Fabric8RuleFactory extends RuleFactory { private final Fabric8NameHelper nameHelper; @@ -44,4 +44,9 @@ public NameHelper getNameHelper() { public Rule getObjectRule() { return new Fabric8ObjectRule(this, new ParcelableHelper(), getReflectionHelper()); } + + @Override + public Rule getDefaultRule() { + return new Fabric8DefaultRule(this); + } } diff --git a/kubernetes-model-generator/pkg/schemagen/generate.go b/kubernetes-model-generator/pkg/schemagen/generate.go index 713d761ae24..b8b6e8c6285 100644 --- a/kubernetes-model-generator/pkg/schemagen/generate.go +++ b/kubernetes-model-generator/pkg/schemagen/generate.go @@ -418,6 +418,7 @@ func (g *schemaGenerator) getPropertyDescriptor(t reflect.Type, desc string, omi JSONDescriptor: &JSONDescriptor{ Type: "object", Description: desc, + JavaOmitEmpty: omitEmpty, }, JSONMapDescriptor: &JSONMapDescriptor{ MapValueType: g.getPropertyDescriptor(t.Elem(), desc, false),