Skip to content

Commit 7d35293

Browse files
committed
[#106] Remove tenant-id from paths if resource path is explicitly provided
* if resource path is not provided, then the path should be of format /{tenant-id}/{aspect name in camel case}. Signed-off-by: Kartheeswaran Kalidass <[email protected]>
1 parent 5ba4bd2 commit 7d35293

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

core/sds-aspect-model-document-generators/src/main/java/io/openmanufacturing/sds/aspectmodel/generator/openapi/AspectModelOpenApiGenerator.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class AspectModelOpenApiGenerator {
7979
private static final String NOT_FOUND_ERROR = "NotFoundError";
8080
private static final String OPERATIONS_SERVER_PATH = "/rpc-api/%s";
8181
private static final String TENANT_ID = "/{tenant-id}";
82-
private static final String OPERATIONS_ENDPOINT_PATH = TENANT_ID + "/%s/operations";
82+
private static final String OPERATIONS_ENDPOINT_PATH = "%s/operations";
8383
private static final String PARAMETER_CONVENTION = "^[a-zA-Z][a-zA-Z0-9-_]*";
8484
private static final String QUERY_SERVER_PATH = "/query-api/%s";
8585
private static final String READ_SERVER_PATH = "/api/%s";
@@ -348,21 +348,25 @@ private ObjectNode getPathsNode( final Aspect aspect, final String baseUrl, fina
348348
final Optional<JsonNode> jsonProperties, final boolean includeQueryApi, final Optional<PagingOption> selectedPagingOption ) throws IOException {
349349
final ObjectNode endpointPathsNode = factory.objectNode();
350350
final ObjectNode pathNode = factory.objectNode();
351-
final String finalResourcePath = resourcePath.orElse( deriveResourcePathFromAspectName( aspect.getName() ) );
352351
final ObjectNode propertiesNode = getPropertiesNode( resourcePath, jsonProperties );
353-
endpointPathsNode.set( TENANT_ID + "/" + finalResourcePath, pathNode );
352+
// If resource path is provided then use it as the complete path and don't prefix tenant-id to it.
353+
final String finalResourcePath = resourcePath
354+
.map( path -> path.startsWith( "/" ) ? path : "/" + path )
355+
.orElse( TENANT_ID + "/" + deriveResourcePathFromAspectName( aspect.getName() ) );
356+
357+
endpointPathsNode.set( finalResourcePath, pathNode );
354358

355359
if ( includePaging( aspect, selectedPagingOption ) ) {
356360
pagingGenerator.setPagingProperties( aspect, selectedPagingOption, propertiesNode );
357361
}
358362

359-
pathNode.set( FIELD_GET, getRequestEndpointsRead( aspect, propertiesNode ) );
363+
pathNode.set( FIELD_GET, getRequestEndpointsRead( aspect, propertiesNode, resourcePath ) );
360364

361365
if ( includeQueryApi ) {
362-
pathNode.set( FIELD_POST, getRequestEndpointFilter( aspect, propertiesNode, baseUrl, apiVersion ) );
366+
pathNode.set( FIELD_POST, getRequestEndpointFilter( aspect, propertiesNode, baseUrl, apiVersion, resourcePath ) );
363367
}
364368

365-
final Optional<ObjectNode> operationsNode = getRequestEndpointOperations( aspect, propertiesNode, baseUrl, apiVersion );
369+
final Optional<ObjectNode> operationsNode = getRequestEndpointOperations( aspect, propertiesNode, baseUrl, apiVersion, resourcePath );
366370
operationsNode.ifPresent(
367371
jsonNodes -> endpointPathsNode
368372
.set( String.format( OPERATIONS_ENDPOINT_PATH, finalResourcePath ), jsonNodes ) );
@@ -374,14 +378,14 @@ private String deriveResourcePathFromAspectName( final String aspectName ) {
374378
}
375379

376380
private Optional<ObjectNode> getRequestEndpointOperations( final Aspect aspect, final ObjectNode parameterNode, final String baseUrl,
377-
final String apiVersion ) {
381+
final String apiVersion, final Optional<String> resourcePath ) {
378382
if ( !aspect.getOperations().isEmpty() ) {
379383
final ObjectNode postNode = factory.objectNode();
380384
final ObjectNode objectNode = factory.objectNode();
381385
setServers( objectNode, baseUrl, apiVersion, OPERATIONS_SERVER_PATH );
382386
objectNode.set( "tags", factory.arrayNode().add( aspect.getName() ) );
383387
objectNode.put( FIELD_OPERATION_ID, FIELD_POST + FIELD_OPERATION + aspect.getName() );
384-
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode ) );
388+
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode, resourcePath.isEmpty() ) );
385389
objectNode.set( "requestBody", factory.objectNode().put( REF, COMPONENTS_REQUESTS + FIELD_OPERATION ) );
386390
final ObjectNode responseNode = factory.objectNode();
387391
objectNode.set( FIELD_RESPONSES, responseNode );
@@ -445,12 +449,13 @@ private ObjectNode getResponseSchemaForOperation( final Optional<Property> prope
445449
return schemaNode;
446450
}
447451

448-
private ObjectNode getRequestEndpointFilter( final Aspect aspect, final ObjectNode parameterNode, final String baseUrl, final String apiVersion ) {
452+
private ObjectNode getRequestEndpointFilter( final Aspect aspect, final ObjectNode parameterNode, final String baseUrl, final String apiVersion,
453+
final Optional<String> resourcePath ) {
449454
final ObjectNode objectNode = factory.objectNode();
450455
setServers( objectNode, baseUrl, apiVersion, QUERY_SERVER_PATH );
451456
objectNode.set( "tags", factory.arrayNode().add( aspect.getName() ) );
452457
objectNode.put( FIELD_OPERATION_ID, FIELD_POST + aspect.getName() );
453-
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode ) );
458+
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode, resourcePath.isEmpty() ) );
454459
objectNode.set( "requestBody", getRequestBodyForFilter() );
455460
objectNode.set( FIELD_RESPONSES, getResponsesForGet( aspect ) );
456461
return objectNode;
@@ -460,11 +465,11 @@ private ObjectNode getRequestBodyForFilter() {
460465
return factory.objectNode().put( REF, COMPONENTS_REQUESTS + FIELD_FILTER );
461466
}
462467

463-
private ObjectNode getRequestEndpointsRead( final Aspect aspect, final ObjectNode parameterNode ) {
468+
private ObjectNode getRequestEndpointsRead( final Aspect aspect, final ObjectNode parameterNode, final Optional<String> resourcePath ) {
464469
final ObjectNode objectNode = factory.objectNode();
465470
objectNode.set( "tags", factory.arrayNode().add( aspect.getName() ) );
466471
objectNode.put( FIELD_OPERATION_ID, FIELD_GET + aspect.getName() );
467-
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode ) );
472+
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode, resourcePath.isEmpty() ) );
468473
objectNode.set( FIELD_RESPONSES, getResponsesForGet( aspect ) );
469474
return objectNode;
470475
}
@@ -491,9 +496,11 @@ private ObjectNode getSuccessfulNode( final Aspect aspect ) {
491496
return factory.objectNode().put( REF, COMPONENTS_RESPONSES + aspect.getName() );
492497
}
493498

494-
private ArrayNode getRequiredParameters( final ObjectNode parameterNode ) {
499+
private ArrayNode getRequiredParameters( final ObjectNode parameterNode, final boolean includeTenantIdNode ) {
495500
final ArrayNode parameters = factory.arrayNode();
496-
parameters.add( getTenantIdNode() );
501+
if(includeTenantIdNode) {
502+
parameters.add( getTenantIdNode() );
503+
}
497504
parameterNode.forEach( parameters::add );
498505
return parameters;
499506
}
@@ -538,4 +545,4 @@ private void handleRecursiveSchemas( final ObjectNode aspectSchema, final Object
538545
}
539546
}
540547
}
541-
}
548+
}

core/sds-aspect-model-document-generators/src/test/java/io/openmanufacturing/sds/aspectmodel/generator/openapi/OpenApiTest.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ public void testIncludeQueryApiWithSemanticVersion( final KnownVersion metaModel
102102
testResourcePath, Optional.empty(), true, Optional.empty() );
103103
final SwaggerParseResult result = new OpenAPIParser().readContents( json.toString(), null, null );
104104
final OpenAPI openAPI = result.getOpenAPI();
105-
106-
assertThat( openAPI.getPaths().get( "/{tenant-id}/" + testResourcePath.get() ).getPost().getServers().get( 0 ).getUrl() )
105+
assertThat( openAPI.getPaths().get( "/" + testResourcePath.get() ).getPost().getServers().get( 0 ).getUrl() )
107106
.isEqualTo( "https://test-aspect.example.com/query-api/v1.0.0" );
108107
}
109108

@@ -116,7 +115,7 @@ public void testDefaultResourcePath( final KnownVersion metaModelVersion ) {
116115
final SwaggerParseResult result = new OpenAPIParser().readContents( json.toString(), null, null );
117116
final OpenAPI openAPI = result.getOpenAPI();
118117

119-
assertThat( openAPI.getPaths().keySet() ).allMatch( path -> path.endsWith( "/aspect-without-see-attribute" ) );
118+
assertThat( openAPI.getPaths().keySet() ).allMatch( path -> path.equals( "/{tenant-id}/aspect-without-see-attribute" ) );
120119
}
121120

122121
@ParameterizedTest
@@ -129,6 +128,17 @@ public void testInvalidResourcePath( final KnownVersion metaModelVersion ) {
129128
assertThat( json ).isEmpty();
130129
}
131130

131+
@ParameterizedTest
132+
@MethodSource( value = "allVersions" )
133+
public void testWithValidResourcePath( final KnownVersion metaModelVersion ) {
134+
final Aspect aspect = loadAspect( TestAspect.ASPECT_WITHOUT_SEE_ATTRIBUTE, metaModelVersion );
135+
final JsonNode json = apiJsonGenerator.applyForJson( aspect, true, testBaseUrl, testResourcePath, Optional.empty(), true, Optional.empty() );
136+
final SwaggerParseResult result = new OpenAPIParser().readContents( json.toString(), null, null );
137+
final OpenAPI openAPI = result.getOpenAPI();
138+
139+
assertThat( openAPI.getPaths().keySet() ).allMatch( path -> path.equals( "/" + testResourcePath.get() ) );
140+
}
141+
132142
@ParameterizedTest
133143
@MethodSource( value = "allVersions" )
134144
public void testInvalidJsonParameter( final KnownVersion metaModelVersion ) {
@@ -151,11 +161,11 @@ public void testValidParameter( final KnownVersion metaModelVersion ) throws IOE
151161

152162
final OpenAPI openAPI = result.getOpenAPI();
153163
assertThat( openAPI.getPaths() ).hasSize( 1 );
154-
assertThat( openAPI.getPaths().keySet() ).contains( "/{tenant-id}/my-test-aspect/{test-Id}" );
164+
assertThat( openAPI.getPaths().keySet() ).contains( "/my-test-aspect/{test-Id}" );
155165
openAPI.getPaths().forEach( ( key, value ) -> {
156166
final List<String> params = value.getGet().getParameters().stream().map( Parameter::getName )
157167
.collect( Collectors.toList() );
158-
assertThat( params ).contains( "tenant-id" );
168+
assertThat( params ).doesNotContain( "tenant-id" );
159169
assertThat( params ).contains( "test-Id" );
160170
} );
161171
}
@@ -395,7 +405,7 @@ private void validateOpenApiSpec( final JsonNode node, final OpenAPI openAPI, fi
395405
assertThat( openAPI.getPaths().keySet() ).noneMatch( path -> path.contains( "/query-api" ) );
396406
openAPI.getPaths().entrySet().stream().filter( item -> !item.getKey().contains( "/operations" ) )
397407
.forEach( path -> {
398-
assertThat( path.getKey() ).startsWith( "/{tenant-id}/" + testResourcePath.get() );
408+
assertThat( path.getKey() ).startsWith( "/" + testResourcePath.get() );
399409
path.getValue().readOperations()
400410
.forEach( operation -> validateSuccessfulResponse( aspect.getName(), operation ) );
401411
} );

0 commit comments

Comments
 (0)