diff --git a/codegen-web/src/test/java/com/networknt/codegen/handler/GeneratorServiceHandlerTest.java b/codegen-web/src/test/java/com/networknt/codegen/handler/GeneratorServiceHandlerTest.java index d436344cb..13f2ee864 100644 --- a/codegen-web/src/test/java/com/networknt/codegen/handler/GeneratorServiceHandlerTest.java +++ b/codegen-web/src/test/java/com/networknt/codegen/handler/GeneratorServiceHandlerTest.java @@ -382,6 +382,7 @@ public void run() { } int statusCode = reference.get().getResponseCode(); String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY); + System.out.println(body); Assert.assertEquals(200, statusCode); Assert.assertNotNull(body); } diff --git a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java index 7d8e0ca14..57c9e65b3 100644 --- a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java +++ b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java @@ -801,13 +801,17 @@ private static String getBasePath(OpenApi3 openApi3) { } // method used to generate valid enum keys for enum contents - private static void attachValidEnumName(Map.Entry entryElement) { + private void attachValidEnumName(Map.Entry entryElement) { Iterator iterator = entryElement.getValue().iterator(); Map map = new HashMap<>(); while (iterator.hasNext()) { String string = iterator.next().toString().trim(); if (string.equals("")) continue; - map.put(convertToValidJavaVariableName(string).toUpperCase(), Any.wrap(string)); + if (isEnumHasDescription(string)) { + map.put(convertToValidJavaVariableName(getEnumName(string)).toUpperCase(), Any.wrap(getEnumDescription(string))); + } else { + map.put(convertToValidJavaVariableName(string).toUpperCase(), Any.wrap(string)); + } } entryElement.setValue(Any.wrap(map)); } @@ -839,6 +843,26 @@ public static String convertToValidJavaVariableName(String string) { return stringBuilder.toString(); } + private boolean isEnumHasDescription(String string) { + return string.contains(":") || string.contains("{") || string.contains("("); + } + + private String getEnumName(String string) { + if (string.contains(":")) return string.substring(0, string.indexOf(":")).trim(); + if (string.contains("(") && string.contains(")")) return string.substring(0, string.indexOf("(")).trim(); + if (string.contains("{") && string.contains("}")) return string.substring(0, string.indexOf("{")).trim(); + return string; + } + + private String getEnumDescription(String string) { + if (string.contains(":")) return string.substring(string.indexOf(":") + 1).trim(); + if (string.contains("(") && string.contains(")")) return string.substring(string.indexOf("(") + 1, string.indexOf(")")).trim(); + if (string.contains("{") && string.contains("}")) return string.substring(string.indexOf("{") + 1, string.indexOf("}")).trim(); + + return string; + } + + private String populateRequestBodyExample(Operation operation) { String result = "{\"content\": \"request body to be replaced\"}"; RequestBody body = operation.getRequestBody(); diff --git a/light-rest-4j/src/test/java/com/networknt/codegen/OpenApiGeneratorTest.java b/light-rest-4j/src/test/java/com/networknt/codegen/OpenApiGeneratorTest.java index 590d5994f..f072ee24e 100644 --- a/light-rest-4j/src/test/java/com/networknt/codegen/OpenApiGeneratorTest.java +++ b/light-rest-4j/src/test/java/com/networknt/codegen/OpenApiGeneratorTest.java @@ -28,6 +28,7 @@ public class OpenApiGeneratorTest { public static String configName = "/config.json"; public static String openapiJson = "/openapi.json"; public static String openapiYaml = "/openapi.yaml"; + public static String openapiEnumYaml = "/openapi-enum.yaml"; public static String openapiNoServersYaml = "/openapi-noServers.yaml"; public static String packageName = "com.networknt.petstore.model"; @@ -97,4 +98,12 @@ public void testConvertInvalidVariableName() { Assert.assertEquals(validVariableNames[i], string); } } + + @Test + public void testGeneratorYamlEnum() throws IOException { + Any anyConfig = JsonIterator.parse(OpenApiGeneratorTest.class.getResourceAsStream(configName), 1024).readAny(); + String strModel = new Scanner(OpenApiGeneratorTest.class.getResourceAsStream(openapiEnumYaml), "UTF-8").useDelimiter("\\A").next(); + OpenApiGenerator generator = new OpenApiGenerator(); + generator.generate(targetPath, strModel, anyConfig); + } } diff --git a/light-rest-4j/src/test/resources/openapi-enum.yaml b/light-rest-4j/src/test/resources/openapi-enum.yaml new file mode 100644 index 000000000..868559e54 --- /dev/null +++ b/light-rest-4j/src/test/resources/openapi-enum.yaml @@ -0,0 +1,175 @@ +openapi: 3.0.0 +info: + version: 1.0.0 + title: Cashflow API + description: Cashflow API - retrieve aggregated cashflow by account + license: + name: Apache 2.0 + url: 'http://www.apache.org/licenses/LICENSE-2.0.html' +servers: + - url: 'http://api.example.com/v1' +paths: + /cashflow: + post: + requestBody: + description: >- + List of account identifiers that contains the account number, + transit and product type code of the accounts to perform aggregation on. + required: true + content: + application/json: + schema: + type: array + maxItems: 15 + items: + $ref: '#/components/schemas/AccountId' + responses: + '200': + description: Cashflow information returned for the matching Account IDs + content: + application/json: + schema: + properties: + aggregatedAsOfdate: + description: The cut-off date of the aggregation. + type: string + format: date + accounts: + description: the account where the aggregation is taking place + type: array + items: + $ref: '#/components/schemas/Accounts' + '404': + description: No transaction information has been found for the accounts + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + '500': + description: Internal Server Error, couldn't retrieve transaction information + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + security: + - accounts_auth: + - cashflow.read + +components: + securitySchemes: + accounts_auth: + type: oauth2 + description: This API uses OAuth 2 with the clientCredentials grant flow. + flows: + clientCredentials: + tokenUrl: http://localhost:8888/oauth2/token + scopes: + cashflow.read: read access - grant to cashflow data retrieval service + schemas: + AccountId: + type: object + required: + - accountNumber + - productTypeCode + properties: + accountNumber: + type: string + transit: + type: string + minLength: 5 + maxLength: 5 + nullable: true + productTypeCode: + type: string + Accounts: + type: object + required: + - accountNumber + - productTypeCode + - resultStatus + properties: + accountNumber: + type: string + transit: + type: string + minLength: 5 + maxLength: 5 + nullable: true + productTypeCode: + type: string + resultStatus: + description: >- + SUCCESS: The aggregation is return successfully.
+ NOT_FOUND: No transactions were found for the account provided. This is because either there
+ were no transactions during the range period we are looking for or the account does not exist.
+ TIMEOUT: The service unable to retrieve the transactions in time for this account
+ based the SLA defined by the business unit
+ ERROR: Transactions were found but there was an error retrieving the transaction + type: string + enum: + - SUCCESS + - NOT_FOUND + - TIMEOUT + - ERROR + incomes: + description: >- + The list of categories for incomes. this field will only be returned if the resultStatus is SUCCESS. + type: array + items: + $ref: '#/components/schemas/Income' + expenses: + description: >- + The list of categories for expenses. this field will only be returned if the resultStatus is SUCCESS. + type: array + items: + $ref: '#/components/schemas/Expense' + Income: + type: object + required: + - categoryIdentifier + - aggregatedAmount + properties: + categoryIdentifier: + description: Category name + type: string + enum: + - CPP : Canada pension plan + - OAS : Ontario Age Service + aggregatedAmount: + type: number + format: int64 + Expense: + type: object + required: + - categoryIdentifier + - aggregatedAmount + properties: + categoryIdentifier: + description: Category name + type: string + enum: + - EFT_BillPay (EFT and Bill Payment) + - Cheque_POS (Cheques and Point Sale) + - ABM_Cashwid (ABM and Cash Withdrawals) + - Loan (Loan Repayment) + - Mortgage (Morgage Repayment) + aggregatedAmount: + type: number + format: int64 + Error: + type: object + required: + - statusCode + - code + - message + - description + properties: + statusCode: + type: integer + format: int32 + code: + type: string + message: + type: string + description: + type: string