Skip to content

Commit

Permalink
fix fabric8io#3625: adding defaults for maps
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed May 18, 2022
1 parent 51cc99a commit b62b844
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -44,4 +44,9 @@ public NameHelper getNameHelper() {
public Rule<JPackage, JType> getObjectRule() {
return new Fabric8ObjectRule(this, new ParcelableHelper(), getReflectionHelper());
}

@Override
public Rule<JFieldVar, JFieldVar> getDefaultRule() {
return new Fabric8DefaultRule(this);
}
}
1 change: 1 addition & 0 deletions kubernetes-model-generator/pkg/schemagen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit b62b844

Please sign in to comment.