|
11 | 11 | import java.io.UnsupportedEncodingException;
|
12 | 12 | import java.net.URI;
|
13 | 13 | import java.net.URLDecoder;
|
| 14 | +import java.util.HashSet; |
| 15 | +import java.util.LinkedHashSet; |
14 | 16 | import java.util.Optional;
|
| 17 | +import java.util.Set; |
15 | 18 | import org.jsonschema2pojo.Jsonschema2Pojo;
|
16 | 19 | import org.jsonschema2pojo.Schema;
|
17 | 20 | import org.jsonschema2pojo.exception.GenerationException;
|
@@ -99,18 +102,40 @@ private Optional<JType> oneOfType(
|
99 | 102 | Schema parentSchema) {
|
100 | 103 | if (schemaNode.has("oneOf")) {
|
101 | 104 | int i = 0;
|
| 105 | + Set<JType> oneOfClasses = new HashSet<>(); |
102 | 106 | for (JsonNode oneOf : (ArrayNode) schemaNode.get("oneOf")) {
|
103 |
| - apply( |
104 |
| - nodeName, |
105 |
| - oneOf, |
106 |
| - parent, |
107 |
| - generatableType.getPackage(), |
108 |
| - ruleFactory |
109 |
| - .getSchemaStore() |
110 |
| - .create( |
111 |
| - URI.create(parentSchema.getId().toString() + "/oneOf/" + i++), |
112 |
| - ruleFactory.getGenerationConfig().getRefFragmentPathDelimiters())); |
| 107 | + oneOfClasses.add( |
| 108 | + apply( |
| 109 | + nodeName, |
| 110 | + oneOf, |
| 111 | + parent, |
| 112 | + generatableType.getPackage(), |
| 113 | + ruleFactory |
| 114 | + .getSchemaStore() |
| 115 | + .create( |
| 116 | + URI.create(parentSchema.getId().toString() + "/oneOf/" + i++), |
| 117 | + ruleFactory.getGenerationConfig().getRefFragmentPathDelimiters()))); |
113 | 118 | }
|
| 119 | + |
| 120 | + Set<JClass> commonAncestors = null; |
| 121 | + for (JType oneOfClass : oneOfClasses) { |
| 122 | + Set<JClass> ancestors = new LinkedHashSet<>(); |
| 123 | + while (oneOfClass instanceof JClass) { |
| 124 | + JClass parentClass = ((JClass) oneOfClass)._extends(); |
| 125 | + if (parentClass instanceof JClass && !parentClass.name().equals("Object")) { |
| 126 | + ancestors.add(parentClass); |
| 127 | + } |
| 128 | + oneOfClass = parentClass; |
| 129 | + } |
| 130 | + if (commonAncestors == null) { |
| 131 | + commonAncestors = ancestors; |
| 132 | + } else { |
| 133 | + commonAncestors.retainAll(ancestors); |
| 134 | + } |
| 135 | + } |
| 136 | + return commonAncestors.isEmpty() |
| 137 | + ? Optional.empty() |
| 138 | + : Optional.of(commonAncestors.iterator().next()); |
114 | 139 | }
|
115 | 140 | return Optional.empty();
|
116 | 141 | }
|
|
0 commit comments