Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any> entryElement) {
private void attachValidEnumName(Map.Entry<String, Any> entryElement) {
Iterator<Any> iterator = entryElement.getValue().iterator();
Map<String, Any> 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));
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);
}
}
175 changes: 175 additions & 0 deletions light-rest-4j/src/test/resources/openapi-enum.yaml
Original file line number Diff line number Diff line change
@@ -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.</br>
NOT_FOUND: No transactions were found for the account provided. This is because either there</br>
were no transactions during the range period we are looking for or the account does not exist.</br>
TIMEOUT: The service unable to retrieve the transactions in time for this account</br>
based the SLA defined by the business unit</br>
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