|
14 | 14 | import java.util.regex.Matcher; |
15 | 15 | import java.util.regex.Pattern; |
16 | 16 |
|
| 17 | +import com.fasterxml.jackson.annotation.JsonInclude; |
17 | 18 | import io.zenwave360.sdk.utils.AsyncAPIUtils; |
| 19 | +import io.zenwave360.sdk.utils.Maps; |
18 | 20 | import io.zenwave360.sdk.zdl.GeneratedProjectFiles; |
19 | 21 | import org.apache.commons.lang3.ObjectUtils; |
20 | | -import org.apache.commons.lang3.RegExUtils; |
21 | | -import org.apache.commons.lang3.StringUtils; |
22 | 22 | import org.jsonschema2pojo.*; |
23 | 23 | import org.jsonschema2pojo.exception.ClassAlreadyExistsException; |
24 | 24 | import org.jsonschema2pojo.rules.RuleFactory; |
25 | 25 | import org.slf4j.Logger; |
26 | 26 | import org.slf4j.LoggerFactory; |
27 | 27 |
|
28 | | -import com.fasterxml.jackson.annotation.JsonInclude; |
29 | 28 | import com.fasterxml.jackson.core.JsonProcessingException; |
30 | 29 | import com.fasterxml.jackson.databind.ObjectMapper; |
31 | 30 | import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
@@ -151,33 +150,42 @@ public void generateFromNativeFormat(JsonSchema2PojoConfiguration config, Map<St |
151 | 150 | codeModel.build(sourcesWriter, resourcesWriter); |
152 | 151 | } |
153 | 152 |
|
154 | | - private final ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); |
155 | 153 | private final ObjectMapper jsonMapper = new ObjectMapper(); |
156 | 154 |
|
157 | 155 | protected String convertToJson(final Map<String, Object> payload, final String packageName) throws JsonProcessingException { |
158 | | - this.yamlMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); |
159 | | - String yml = this.yamlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(payload); |
160 | | - |
161 | | - List<String> regexPatterns = List.of( |
162 | | - originalRefProperty + ": \".*#/components/schemas/([^\"]+)\"", |
163 | | - "ref: \".*#/components/schemas/([^\"]+)\"" |
164 | | - ); |
165 | | - |
166 | | - for (String regex : regexPatterns) { |
167 | | - Pattern pattern = Pattern.compile(regex); |
168 | | - Matcher matcher = pattern.matcher(yml); |
169 | | - StringBuilder result = new StringBuilder(); |
170 | | - |
171 | | - while (matcher.find()) { |
172 | | - String matchedGroup = matcher.group(1); |
173 | | - String className = NamingUtils.asJavaTypeName(matchedGroup); |
174 | | - matcher.appendReplacement(result, "javaType: \"" + packageName + "." + className + "\""); |
| 156 | + populateJavaTypeFromRefsRecursively(payload, packageName); |
| 157 | + return this.jsonMapper.writeValueAsString(payload); |
| 158 | + } |
| 159 | + |
| 160 | + private void populateJavaTypeFromRefsRecursively(Object obj, String packageName) { |
| 161 | + if (obj instanceof Map) { |
| 162 | + Map<String, Object> map = (Map<String, Object>) obj; |
| 163 | + |
| 164 | + // Replace $ref and original ref with javaType (if not already present) |
| 165 | + if (!map.containsKey("javaType")) { |
| 166 | + String refValue = JSONPath.getFirst(map, "$['" + originalRefProperty + "']", "$['$ref']"); |
| 167 | + |
| 168 | + if (refValue != null && refValue.contains("#/components/schemas/")) { |
| 169 | + String schemaName = refValue.substring(refValue.lastIndexOf("/") + 1); |
| 170 | + String className = NamingUtils.asJavaTypeName(schemaName); |
| 171 | + map.put("javaType", packageName + "." + className); |
| 172 | + } |
| 173 | + } |
| 174 | + map.remove("x--original-$ref"); |
| 175 | + map.remove("$ref"); |
| 176 | + |
| 177 | + |
| 178 | + // Recursively process all values in the map |
| 179 | + for (Object value : map.values()) { |
| 180 | + populateJavaTypeFromRefsRecursively(value, packageName); |
175 | 181 | } |
176 | | - yml = matcher.appendTail(result).toString(); |
177 | | - } |
178 | 182 |
|
179 | | - Object jsonObject = this.yamlMapper.readTree(yml); |
180 | | - return this.jsonMapper.writeValueAsString(jsonObject); |
| 183 | + } else if (obj instanceof List) { |
| 184 | + List<Object> list = (List<Object>) obj; |
| 185 | + for (Object item : list) { |
| 186 | + populateJavaTypeFromRefsRecursively(item, packageName); |
| 187 | + } |
| 188 | + } |
181 | 189 | } |
182 | 190 |
|
183 | 191 | private Annotator instantiate(Class<? extends Annotator> annotatorClass, GenerationConfig config) { |
|
0 commit comments