Skip to content

Commit 60c2170

Browse files
authored
Merge pull request #93 from ZenWave360/mainteinance/v2.2.1
fix: processing of javaType in jsonschema3pojo plugin
2 parents 0ecb7fc + f63dc27 commit 60c2170

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

plugins/asyncapi-jsonschema2pojo/src/main/java/io/zenwave360/sdk/plugins/AsyncApiJsonSchema2PojoGenerator.java

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@
1414
import java.util.regex.Matcher;
1515
import java.util.regex.Pattern;
1616

17+
import com.fasterxml.jackson.annotation.JsonInclude;
1718
import io.zenwave360.sdk.utils.AsyncAPIUtils;
19+
import io.zenwave360.sdk.utils.Maps;
1820
import io.zenwave360.sdk.zdl.GeneratedProjectFiles;
1921
import org.apache.commons.lang3.ObjectUtils;
20-
import org.apache.commons.lang3.RegExUtils;
21-
import org.apache.commons.lang3.StringUtils;
2222
import org.jsonschema2pojo.*;
2323
import org.jsonschema2pojo.exception.ClassAlreadyExistsException;
2424
import org.jsonschema2pojo.rules.RuleFactory;
2525
import org.slf4j.Logger;
2626
import org.slf4j.LoggerFactory;
2727

28-
import com.fasterxml.jackson.annotation.JsonInclude;
2928
import com.fasterxml.jackson.core.JsonProcessingException;
3029
import com.fasterxml.jackson.databind.ObjectMapper;
3130
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
@@ -151,33 +150,42 @@ public void generateFromNativeFormat(JsonSchema2PojoConfiguration config, Map<St
151150
codeModel.build(sourcesWriter, resourcesWriter);
152151
}
153152

154-
private final ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
155153
private final ObjectMapper jsonMapper = new ObjectMapper();
156154

157155
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);
175181
}
176-
yml = matcher.appendTail(result).toString();
177-
}
178182

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+
}
181189
}
182190

183191
private Annotator instantiate(Class<? extends Annotator> annotatorClass, GenerationConfig config) {

plugins/asyncapi-jsonschema2pojo/src/test/java/io/zenwave360/sdk/plugins/AsyncApiJsonSchema2PojoGeneratorTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public void test_generator_for_asyncapi_v3() throws Exception {
3030
new MainGenerator().generate(plugin);
3131

3232
Assertions.assertTrue(new File("target/zenwave630/src/main/java/io/example/v3/domain/events/CustomerInput.java").exists());
33+
Assertions.assertTrue(new File("target/zenwave630/src/main/java/mypackage/CustomerCreated.java").exists());
34+
Assertions.assertFalse(new File("target/zenwave630/src/main/java/io/example/v3/domain/events/CustomerCreated.java").exists());
3335
}
3436

3537
@Test

plugins/asyncapi-jsonschema2pojo/src/test/resources/asyncapi-v3.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ components:
137137
CustomerCreated:
138138
type: "object"
139139
x-business-entity: "CustomerCreated"
140+
javaType: "mypackage.CustomerCreated"
140141
properties:
141142
id:
142143
type: "string"

0 commit comments

Comments
 (0)