Skip to content

Commit 50474b4

Browse files
committed
Added fixes for duplicate enums and dashes in enum constants.
1 parent c8c3d0f commit 50474b4

File tree

6 files changed

+112
-17
lines changed

6 files changed

+112
-17
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Java Client Library (beta)
2-
### v0.4.2
2+
3+
### Latest snapshot version v0.4.3-SNAPSHOT
4+
### Latest released version v0.4.2
35

46
## Introduction
57
Welcome my friends! This is the Poly API Java client GitHub page. If you are here, then it means you're familiar with what we do at Poly. If you aren't, you can always check [here](https://github.com/polyapi/poly-alpha).
@@ -369,7 +371,8 @@ These features will be added in the future releases.
369371

370372
## Changelog
371373
### v0.4.2
372-
- TBD
374+
- Fixed code generation bug where enum constants had dashes in them '-'.
375+
- Fixed code generation bug where multiple classes within a package had the same enum declaration.
373376
### v0.4.1
374377
- Added Function ID in the javadocs of functions.
375378
### v0.4.0

library/src/main/java/io/polyapi/client/internal/service/VariableInjectionServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Object replace(String propertyName, Object original) {
3030
}
3131

3232
public synchronized <T> T inject(String key, String type) {
33-
logger.error("Injecting variable with key '{}' and type '{}'.", key, type);
33+
logger.debug("Injecting variable with key '{}' and type '{}'.", key, type);
3434
if (!injectionMap.containsKey(key)) {
3535
logger.debug("Injection map doesn't contain the key, generating a new one.");
3636
injectionMap.put(key, switch (type.toLowerCase()) {

polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/schema/PublicEnumRule.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.polyapi.plugin.service.schema;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.node.ObjectNode;
45
import com.sun.codemodel.*;
6+
import org.json.JSONObject;
57
import org.jsonschema2pojo.GenerationConfig;
68
import org.jsonschema2pojo.Jackson2Annotator;
79
import org.jsonschema2pojo.Schema;
@@ -12,17 +14,37 @@
1214
import org.jsonschema2pojo.rules.EnumRule;
1315
import org.jsonschema2pojo.rules.RuleFactory;
1416
import org.jsonschema2pojo.util.NameHelper;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
1519

20+
import java.util.Optional;
21+
import java.util.Spliterator;
22+
import java.util.Spliterators;
23+
import java.util.stream.Stream;
24+
import java.util.stream.StreamSupport;
25+
26+
import static java.util.Spliterator.ORDERED;
27+
import static java.util.Spliterators.spliteratorUnknownSize;
28+
import static java.util.stream.StreamSupport.stream;
1629
import static org.jsonschema2pojo.rules.PrimitiveTypes.isPrimitive;
1730

1831
public class PublicEnumRule extends EnumRule {
32+
private static final Logger logger = LoggerFactory.getLogger(PublicEnumRule.class);
1933

2034
protected PublicEnumRule(RuleFactory ruleFactory) {
2135
super(ruleFactory);
2236
}
2337

2438
@Override
2539
public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContainer container, Schema schema) {
26-
return super.apply(nodeName, node, parent, container.getPackage(), schema);
40+
return stream(Spliterators.<JType>spliteratorUnknownSize(container.getPackage().classes(), ORDERED), false)
41+
.filter(definedClass -> definedClass.name().equalsIgnoreCase(nodeName))
42+
.findFirst()
43+
.orElseGet(() -> super.apply(nodeName, node, parent, container.getPackage(), schema));
44+
}
45+
46+
@Override
47+
protected String getConstantName(String nodeName, String customName) {
48+
return super.getConstantName(nodeName.replace("-", "_"), customName);
2749
}
2850
}

polyapi-maven-plugin/src/test/java/io/polyapi/plugin/service/JsonSchemaParserTest.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.junit.jupiter.params.provider.MethodSource;
1818

1919
import java.io.IOException;
20+
import java.util.Arrays;
2021
import java.util.List;
2122
import java.util.Optional;
2223
import java.util.stream.Stream;
@@ -34,28 +35,34 @@ public class JsonSchemaParserTest {
3435
private final ObjectMapper objectMapper = new ObjectMapper();
3536

3637
public static Stream<Arguments> generateSource() {
37-
return Stream.of(Arguments.of("Simple recursive schema with no base type.", "Case 1", 2, List.of("Data", DEFAULT_RESPONSE_NAME)),
38-
Arguments.of("Recursive schema with base type.", "Case 2", 8, List.of("Coupon", "Discount", "Address", "CouponMetadata", "ResponseTypeMetadata", DEFAULT_RESPONSE_NAME, "InvoiceSettings", "Shipping")),
39-
Arguments.of("Schema that has a text value evaluated to null.", "Case 3", 8, List.of("Message", DEFAULT_RESPONSE_NAME, "Edited", "Metadata", "Block", "Attachment", "Text", "EventPayload")),
40-
Arguments.of("Schema with base type and no definitions.", "Case 4", 1, List.of(DEFAULT_RESPONSE_NAME)),
41-
Arguments.of("Schema for array of numbers.", "Case 5", 1, List.of(DEFAULT_RESPONSE_NAME)),
42-
Arguments.of("Schema for array of integers.", "Case 6", 1, List.of(DEFAULT_RESPONSE_NAME)),
43-
Arguments.of("Simple schema with attribute.", "Case 7", 1, List.of(DEFAULT_RESPONSE_NAME)),
44-
Arguments.of("Schema with duplicate fields.", "Case 8", 1, List.of("ResponseTypeElement")),
45-
Arguments.of("Schema with enum.", "Case 9", 2, List.of("TestResponse", "DashStyle")),
46-
Arguments.of("Schema that is a String.", "Case 10", 0, List.of()),
47-
Arguments.of("Schema that uses allof.", "Case 11", 0, List.of()));
38+
return Stream.of(createArguments(1, "Simple recursive schema with no base type.", "Data", DEFAULT_RESPONSE_NAME),
39+
createArguments(2, "Recursive schema with base type.", "Coupon", "Discount", "Address", "CouponMetadata", "ResponseTypeMetadata", DEFAULT_RESPONSE_NAME, "InvoiceSettings", "Shipping"),
40+
createArguments(3, "Schema that has a text value evaluated to null.", "Message", DEFAULT_RESPONSE_NAME, "Edited", "Metadata", "Block", "Attachment", "Text", "EventPayload"),
41+
createArguments(4, "Schema with base type and no definitions.", DEFAULT_RESPONSE_NAME),
42+
createArguments(5, "Schema for array of numbers.", DEFAULT_RESPONSE_NAME),
43+
createArguments(6, "Schema for array of integers.", DEFAULT_RESPONSE_NAME),
44+
createArguments(7, "Simple schema with attribute.", DEFAULT_RESPONSE_NAME),
45+
createArguments(8, "Schema with duplicate fields.", "ResponseTypeElement"),
46+
createArguments(9, "Schema with enum.", "TestResponse", "DashStyle"),
47+
createArguments(10, "Schema that is a String."),
48+
createArguments(11, "Schema that uses allof."),
49+
createArguments(12, "Schema with enum with '-' in one of the options.", "Identifier", "TestResponse"),
50+
createArguments(13, "Schema with different types that have the same enum.", "Identifier", "TestResponse", "Data"));
51+
}
52+
53+
private static Arguments createArguments(Integer caseNumber, String caseDescription, String... expectedNames) {
54+
return Arguments.of(caseDescription, format("Case %s", caseNumber), Arrays.stream(expectedNames).toList());
4855
}
4956

5057
@ParameterizedTest(name = "{1}: {0}")
5158
@MethodSource("generateSource")
52-
public void generateTest(String description, String schemaFileName, Integer expectedSize, List<String> expectedNames) throws IOException {
59+
public void generateTest(String description, String schemaFileName, List<String> expectedNames) throws IOException {
5360
var specification = new ApiFunctionSpecification();
5461
specification.setName("test");
5562
specification.setContext("polyapi.testing");
5663
var customTypes = jsonSchemaParser.parse("TestResponse", specification.getPackageName(), IOUtils.toString(JsonSchemaParser.class.getResourceAsStream(format("/%s/cases/%s.schema.json", JsonSchemaParser.class.getPackageName().replace(".", "/"), schemaFileName)), defaultCharset()));
5764
assertThat(customTypes, notNullValue());
58-
assertThat(customTypes.size(), equalTo(expectedSize));
65+
assertThat(customTypes.size(), equalTo(expectedNames.size()));
5966
var customTypeNames = customTypes.stream().map(CustomType::getName).toList();
6067
expectedNames.forEach(expectedName -> assertTrue(customTypeNames.contains(expectedName), format("Result should contain object with name %s. Result contains %s.", expectedName, customTypeNames)));
6168
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "https://json-schema.org/draft-06/schema#",
3+
"type": "object",
4+
"properties": {
5+
"identifier": {
6+
"type": "string",
7+
"description": "Record identifier",
8+
"default": "current",
9+
"enum": [
10+
"current",
11+
"co-applicant"
12+
]
13+
}
14+
},
15+
"additionalProperties": false
16+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-06/schema#",
3+
"definitions": {
4+
"Data": {
5+
"type": "object",
6+
"additionalProperties": false,
7+
"properties": {
8+
"hello": {
9+
"type": "string"
10+
},
11+
"identifier": {
12+
"type": "string",
13+
"description": "Record identifier",
14+
"default": "current",
15+
"enum": [
16+
"current",
17+
"co-applicant"
18+
]
19+
}
20+
},
21+
"required": [
22+
"hello"
23+
],
24+
"title": "Data"
25+
}
26+
},
27+
"type": "object",
28+
"additionalProperties": false,
29+
"properties": {
30+
"data": {
31+
"$ref": "#/definitions/Data"
32+
},
33+
"identifier": {
34+
"type": "string",
35+
"description": "Record identifier",
36+
"default": "current",
37+
"enum": [
38+
"current",
39+
"co-applicant"
40+
]
41+
}
42+
},
43+
"required": [
44+
"data"
45+
],
46+
"title": "ResponseType"
47+
}

0 commit comments

Comments
 (0)