Skip to content

Commit

Permalink
Fix nested openApi objects cause deploy issue with ApiGateway
Browse files Browse the repository at this point in the history
  • Loading branch information
matusfaro committed Nov 5, 2024
1 parent db62ba5 commit 23a3082
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 53 deletions.
106 changes: 56 additions & 50 deletions dataspray-api-parent/dataspray-api/src/main/openapi/paths-control.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,59 +49,65 @@ components:
- JAVA21
- NODEJS20_X
endpoint:
type: object
required:
- isPublic
properties:
isPublic:
type: boolean
cors:
type: object
required:
- allowOrigins
- allowMethods
- allowHeaders
- exposeHeaders
- allowCredentials
- maxAge
properties:
allowOrigins:
type: array
items:
type: string
allowMethods:
type: array
items:
type: string
allowHeaders:
type: array
items:
type: string
exposeHeaders:
type: array
items:
type: string
allowCredentials:
type: boolean
maxAge:
type: integer
format: int64
$ref: '#/components/schemas/DeployRequestEndpoint'
dynamoState:
type: object
required:
- lsiCount
- gsiCount
properties:
lsiCount:
type: integer
format: int64
maximum: 10
gsiCount:
type: integer
format: int64
maximum: 10
$ref: '#/components/schemas/DeployRequestDynamoState'
switchToNow:
type: boolean
DeployRequestEndpoint:
type: object
required:
- isPublic
properties:
isPublic:
type: boolean
cors:
$ref: '#/components/schemas/DeployRequestEndpointCors'
DeployRequestEndpointCors:
type: object
required:
- allowOrigins
- allowMethods
- allowHeaders
- exposeHeaders
- allowCredentials
- maxAge
properties:
allowOrigins:
type: array
items:
type: string
allowMethods:
type: array
items:
type: string
allowHeaders:
type: array
items:
type: string
exposeHeaders:
type: array
items:
type: string
allowCredentials:
type: boolean
maxAge:
type: integer
format: int64
DeployRequestDynamoState:
type: object
required:
- lsiCount
- gsiCount
properties:
lsiCount:
type: integer
format: int64
maximum: 10
gsiCount:
type: integer
format: int64
maximum: 10
TaskVersions:
type: object
required:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package io.dataspray.api;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.yaml.snakeyaml.Yaml;

Expand All @@ -31,15 +32,21 @@
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ApiConstantsTest {

@Test
void testTopLevelPaths() throws Exception {
Map<String, Object> api;
Map<String, Object> api;

@BeforeEach
void beforeEach() throws Exception {
try (FileReader fileReader = new FileReader("target/generated-sources/api/openapi/openapi.yaml")) {
api = new Yaml().load(fileReader);
}
}

@Test
void testTopLevelPaths() throws Exception {
//noinspection unchecked
Set<String> actual = ((Map<String, Object>) api.get("paths"))
.keySet()
Expand All @@ -53,4 +60,19 @@ void testTopLevelPaths() throws Exception {

assertEquals(Set.copyOf(ApiConstants.TOP_LEVEL_PATHS), actual, "Update TOP_LEVEL_PATHS in ApiConstants");
}

@Test
void testModelNames() throws Exception {
//noinspection unchecked
Map<String, Object> components = (Map<String, Object>) api.get("components");
//noinspection unchecked
Map<String, Object> schemas = (Map<String, Object>) components.get("schemas");
schemas.keySet().forEach(model -> {
// AWS ApiGateway requires all Models to be alphanumeric, but OpenAPI generates names on the fly when you
// nest an object within an object by separating it with an underscore. This test ensures that all models
// are alphanumeric. Otherwise you will get an error during CDK deploy:
// > Unable to create model for 'DeployRequest_dynamoState': Model name must be alphanumeric: DeployRequest_dynamoState
assertTrue(model.matches("^[a-zA-Z0-9]+$"), "Model names must be alphanumeric, found: '" + model + "'. Likely caused by nested objects, decouple and use $ref.");
});
}
}

0 comments on commit 23a3082

Please sign in to comment.