Skip to content

Commit a4c1a73

Browse files
authored
Merge pull request #57 from dvmikheev/fix/javaType-value
fix: use formatted POJO name in converted json for javaType value
2 parents d28bdd0 + bea377c commit a4c1a73

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.net.URISyntaxException;
1212
import java.net.URL;
1313
import java.util.*;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
1416

1517
import io.zenwave360.sdk.utils.AsyncAPIUtils;
1618
import io.zenwave360.sdk.zdl.GeneratedProjectFiles;
@@ -175,8 +177,25 @@ public void generateFromNativeFormat(JsonSchema2PojoConfiguration config, Map<St
175177
protected String convertToJson(final Map<String, Object> payload, final String packageName) throws JsonProcessingException {
176178
this.yamlMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
177179
String yml = this.yamlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(payload);
178-
yml = RegExUtils.replaceAll(yml, originalRefProperty + ": \".*#/components/schemas/", "javaType: \"" + packageName + ".");
179-
yml = RegExUtils.replaceAll(yml, "ref: \".*#/components/schemas/", "javaType: \"" + packageName + ".");
180+
181+
List<String> regexPatterns = List.of(
182+
originalRefProperty + ": \".*#/components/schemas/([^\"]+)\"",
183+
"ref: \".*#/components/schemas/([^\"]+)\""
184+
);
185+
186+
for (String regex : regexPatterns) {
187+
Pattern pattern = Pattern.compile(regex);
188+
Matcher matcher = pattern.matcher(yml);
189+
StringBuilder result = new StringBuilder();
190+
191+
while (matcher.find()) {
192+
String matchedGroup = matcher.group(1);
193+
String className = NamingUtils.asJavaTypeName(matchedGroup);
194+
matcher.appendReplacement(result, "javaType: \"" + packageName + "." + className + "\"");
195+
}
196+
yml = matcher.appendTail(result).toString();
197+
}
198+
180199
Object jsonObject = this.yamlMapper.readTree(yml);
181200
return this.jsonMapper.writeValueAsString(jsonObject);
182201
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ public void test_generator_for_asyncapi_v3() throws Exception {
3232
Assertions.assertTrue(new File("target/zenwave630/src/main/java/io/example/v3/domain/events/CustomerInput.java").exists());
3333
}
3434

35+
@Test
36+
public void test_generator_for_asyncapi_schemas_with_delimiters() throws Exception {
37+
Plugin plugin = new AsyncApiJsonSchema2PojoPlugin()
38+
.withApiFile("classpath:asyncapi.yml")
39+
.withTargetFolder("target/zenwave630")
40+
.withOption("modelPackage", "io.example.integration.test.with_schemas.model");
41+
42+
new MainGenerator().generate(plugin);
43+
44+
Assertions.assertTrue(new File("target/zenwave630/src/main/java/io/example/integration/test/with_schemas/model/DepartmentV1.java").exists());
45+
}
46+
3547
@Test
3648
public void test_generator_for_asyncapi_v3_filter_messages() throws Exception {
3749
Plugin plugin = new AsyncApiJsonSchema2PojoPlugin()

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,19 @@ components:
2424
backupEmail:
2525
description: something else
2626
$ref: "#/components/schemas/Email"
27+
department:
28+
description: department where user works
29+
$ref: "#/components/schemas/Department-v1"
2730
schemas:
2831
Email:
2932
type: string
3033
enum:
3134
- Google
3235
- Yahoo
36+
Department-v1:
37+
type: object
38+
properties:
39+
identifier:
40+
type: string
41+
name:
42+
type: string

0 commit comments

Comments
 (0)