Skip to content

Commit df7b913

Browse files
authored
Merge pull request #241 from bci-oss/#106_Remove_tenant_id_from_resource_path
[#106] Remove tenant-id from paths if resource path is explicitly provided
2 parents d342f1c + 5b69a1f commit df7b913

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

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

Lines changed: 21 additions & 14 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";
@@ -398,21 +398,25 @@ private ObjectNode getPathsNode( final Aspect aspect, final String baseUrl, fina
398398
final Optional<JsonNode> jsonProperties, final boolean includeQueryApi, final Optional<PagingOption> selectedPagingOption ) throws IOException {
399399
final ObjectNode endpointPathsNode = factory.objectNode();
400400
final ObjectNode pathNode = factory.objectNode();
401-
final String finalResourcePath = resourcePath.orElse( deriveResourcePathFromAspectName( aspect.getName() ) );
402401
final ObjectNode propertiesNode = getPropertiesNode( resourcePath, jsonProperties );
403-
endpointPathsNode.set( TENANT_ID + "/" + finalResourcePath, pathNode );
402+
// If resource path is provided then use it as the complete path and don't prefix tenant-id to it.
403+
final String finalResourcePath = resourcePath
404+
.map( path -> path.startsWith( "/" ) ? path : "/" + path )
405+
.orElse( TENANT_ID + "/" + deriveResourcePathFromAspectName( aspect.getName() ) );
406+
407+
endpointPathsNode.set( finalResourcePath, pathNode );
404408

405409
if ( includePaging( aspect, selectedPagingOption ) ) {
406410
pagingGenerator.setPagingProperties( aspect, selectedPagingOption, propertiesNode );
407411
}
408412

409-
pathNode.set( FIELD_GET, getRequestEndpointsRead( aspect, propertiesNode ) );
413+
pathNode.set( FIELD_GET, getRequestEndpointsRead( aspect, propertiesNode, resourcePath ) );
410414

411415
if ( includeQueryApi ) {
412-
pathNode.set( FIELD_POST, getRequestEndpointFilter( aspect, propertiesNode, baseUrl, apiVersion ) );
416+
pathNode.set( FIELD_POST, getRequestEndpointFilter( aspect, propertiesNode, baseUrl, apiVersion, resourcePath ) );
413417
}
414418

415-
final Optional<ObjectNode> operationsNode = getRequestEndpointOperations( aspect, propertiesNode, baseUrl, apiVersion );
419+
final Optional<ObjectNode> operationsNode = getRequestEndpointOperations( aspect, propertiesNode, baseUrl, apiVersion, resourcePath );
416420
operationsNode.ifPresent(
417421
jsonNodes -> endpointPathsNode
418422
.set( String.format( OPERATIONS_ENDPOINT_PATH, finalResourcePath ), jsonNodes ) );
@@ -424,14 +428,14 @@ private String deriveResourcePathFromAspectName( final String aspectName ) {
424428
}
425429

426430
private Optional<ObjectNode> getRequestEndpointOperations( final Aspect aspect, final ObjectNode parameterNode, final String baseUrl,
427-
final String apiVersion ) {
431+
final String apiVersion, final Optional<String> resourcePath ) {
428432
if ( !aspect.getOperations().isEmpty() ) {
429433
final ObjectNode postNode = factory.objectNode();
430434
final ObjectNode objectNode = factory.objectNode();
431435
setServers( objectNode, baseUrl, apiVersion, OPERATIONS_SERVER_PATH );
432436
objectNode.set( "tags", factory.arrayNode().add( aspect.getName() ) );
433437
objectNode.put( FIELD_OPERATION_ID, FIELD_POST + FIELD_OPERATION + aspect.getName() );
434-
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode ) );
438+
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode, resourcePath.isEmpty() ) );
435439
objectNode.set( "requestBody", factory.objectNode().put( REF, COMPONENTS_REQUESTS + FIELD_OPERATION ) );
436440
final ObjectNode responseNode = factory.objectNode();
437441
objectNode.set( FIELD_RESPONSES, responseNode );
@@ -495,12 +499,13 @@ private ObjectNode getResponseSchemaForOperation( final Optional<Property> prope
495499
return schemaNode;
496500
}
497501

498-
private ObjectNode getRequestEndpointFilter( final Aspect aspect, final ObjectNode parameterNode, final String baseUrl, final String apiVersion ) {
502+
private ObjectNode getRequestEndpointFilter( final Aspect aspect, final ObjectNode parameterNode, final String baseUrl, final String apiVersion,
503+
final Optional<String> resourcePath ) {
499504
final ObjectNode objectNode = factory.objectNode();
500505
setServers( objectNode, baseUrl, apiVersion, QUERY_SERVER_PATH );
501506
objectNode.set( "tags", factory.arrayNode().add( aspect.getName() ) );
502507
objectNode.put( FIELD_OPERATION_ID, FIELD_POST + aspect.getName() );
503-
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode ) );
508+
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode, resourcePath.isEmpty() ) );
504509
objectNode.set( "requestBody", getRequestBodyForFilter() );
505510
objectNode.set( FIELD_RESPONSES, getResponsesForGet( aspect ) );
506511
return objectNode;
@@ -510,11 +515,11 @@ private ObjectNode getRequestBodyForFilter() {
510515
return factory.objectNode().put( REF, COMPONENTS_REQUESTS + FIELD_FILTER );
511516
}
512517

513-
private ObjectNode getRequestEndpointsRead( final Aspect aspect, final ObjectNode parameterNode ) {
518+
private ObjectNode getRequestEndpointsRead( final Aspect aspect, final ObjectNode parameterNode, final Optional<String> resourcePath ) {
514519
final ObjectNode objectNode = factory.objectNode();
515520
objectNode.set( "tags", factory.arrayNode().add( aspect.getName() ) );
516521
objectNode.put( FIELD_OPERATION_ID, FIELD_GET + aspect.getName() );
517-
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode ) );
522+
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode, resourcePath.isEmpty() ) );
518523
objectNode.set( FIELD_RESPONSES, getResponsesForGet( aspect ) );
519524
return objectNode;
520525
}
@@ -541,9 +546,11 @@ private ObjectNode getSuccessfulNode( final Aspect aspect ) {
541546
return factory.objectNode().put( REF, COMPONENTS_RESPONSES + aspect.getName() );
542547
}
543548

544-
private ArrayNode getRequiredParameters( final ObjectNode parameterNode ) {
549+
private ArrayNode getRequiredParameters( final ObjectNode parameterNode, final boolean includeTenantIdNode ) {
545550
final ArrayNode parameters = factory.arrayNode();
546-
parameters.add( getTenantIdNode() );
551+
if ( includeTenantIdNode ) {
552+
parameters.add( getTenantIdNode() );
553+
}
547554
parameterNode.forEach( parameters::add );
548555
return parameters;
549556
}

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
@@ -106,8 +106,7 @@ public void testIncludeQueryApiWithSemanticVersion( final KnownVersion metaModel
106106
testResourcePath, Optional.empty(), true, Optional.empty() );
107107
final SwaggerParseResult result = new OpenAPIParser().readContents( json.toString(), null, null );
108108
final OpenAPI openAPI = result.getOpenAPI();
109-
110-
assertThat( openAPI.getPaths().get( "/{tenant-id}/" + testResourcePath.get() ).getPost().getServers().get( 0 ).getUrl() )
109+
assertThat( openAPI.getPaths().get( "/" + testResourcePath.get() ).getPost().getServers().get( 0 ).getUrl() )
111110
.isEqualTo( "https://test-aspect.example.com/query-api/v1.0.0" );
112111
}
113112

@@ -120,7 +119,7 @@ public void testDefaultResourcePath( final KnownVersion metaModelVersion ) {
120119
final SwaggerParseResult result = new OpenAPIParser().readContents( json.toString(), null, null );
121120
final OpenAPI openAPI = result.getOpenAPI();
122121

123-
assertThat( openAPI.getPaths().keySet() ).allMatch( path -> path.endsWith( "/aspect-without-see-attribute" ) );
122+
assertThat( openAPI.getPaths().keySet() ).allMatch( path -> path.equals( "/{tenant-id}/aspect-without-see-attribute" ) );
124123
}
125124

126125
@ParameterizedTest
@@ -133,6 +132,17 @@ public void testInvalidResourcePath( final KnownVersion metaModelVersion ) {
133132
assertThat( json ).isEmpty();
134133
}
135134

135+
@ParameterizedTest
136+
@MethodSource( value = "allVersions" )
137+
public void testWithValidResourcePath( final KnownVersion metaModelVersion ) {
138+
final Aspect aspect = loadAspect( TestAspect.ASPECT_WITHOUT_SEE_ATTRIBUTE, metaModelVersion );
139+
final JsonNode json = apiJsonGenerator.applyForJson( aspect, true, testBaseUrl, testResourcePath, Optional.empty(), true, Optional.empty() );
140+
final SwaggerParseResult result = new OpenAPIParser().readContents( json.toString(), null, null );
141+
final OpenAPI openAPI = result.getOpenAPI();
142+
143+
assertThat( openAPI.getPaths().keySet() ).allMatch( path -> path.equals( "/" + testResourcePath.get() ) );
144+
}
145+
136146
@ParameterizedTest
137147
@MethodSource( value = "allVersions" )
138148
public void testInvalidJsonParameter( final KnownVersion metaModelVersion ) {
@@ -155,11 +165,11 @@ public void testValidParameter( final KnownVersion metaModelVersion ) throws IOE
155165

156166
final OpenAPI openAPI = result.getOpenAPI();
157167
assertThat( openAPI.getPaths() ).hasSize( 1 );
158-
assertThat( openAPI.getPaths().keySet() ).contains( "/{tenant-id}/my-test-aspect/{test-Id}" );
168+
assertThat( openAPI.getPaths().keySet() ).contains( "/my-test-aspect/{test-Id}" );
159169
openAPI.getPaths().forEach( ( key, value ) -> {
160170
final List<String> params = value.getGet().getParameters().stream().map( Parameter::getName )
161171
.collect( Collectors.toList() );
162-
assertThat( params ).contains( "tenant-id" );
172+
assertThat( params ).doesNotContain( "tenant-id" );
163173
assertThat( params ).contains( "test-Id" );
164174
} );
165175
}
@@ -400,7 +410,7 @@ private void validateOpenApiSpec( final JsonNode node, final OpenAPI openAPI, fi
400410
assertThat( openAPI.getPaths().keySet() ).noneMatch( path -> path.contains( "/query-api" ) );
401411
openAPI.getPaths().entrySet().stream().filter( item -> !item.getKey().contains( "/operations" ) )
402412
.forEach( path -> {
403-
assertThat( path.getKey() ).startsWith( "/{tenant-id}/" + testResourcePath.get() );
413+
assertThat( path.getKey() ).startsWith( "/" + testResourcePath.get() );
404414
path.getValue().readOperations()
405415
.forEach( operation -> validateSuccessfulResponse( aspect.getName(), operation ) );
406416
} );

0 commit comments

Comments
 (0)