forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix fabric8io#3625: adding defaults for maps
- Loading branch information
Showing
3 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...onschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8DefaultRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters