Skip to content

Commit

Permalink
Merge pull request #638 from bci-oss/635-Allow-Customization-of-Error…
Browse files Browse the repository at this point in the history
…-Responses-in-OpenAPI-Generator

Allow customization of error responses in open api generator
  • Loading branch information
MelleD committed Aug 8, 2024
2 parents c7b2e4c + 2d3c17f commit 252bc6c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ private static ObjectNode mergeObjectNode( final ObjectNode node, final ObjectNo
node.set( key, mergeObjectNode( (ObjectNode) resultValue, (ObjectNode) value ) );
} else if ( resultValue.isArray() && value.isArray() ) {
node.set( key, mergeArrayNode( (ArrayNode) resultValue, (ArrayNode) value ) );
} else {
node.set( key, value );
}
} else {
node.set( key, value );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static org.eclipse.esmf.aspectmodel.generator.openapi.AspectModelOpenApiGenerator.ObjectNodeExtension.getter;

import java.util.Locale;
import java.util.Optional;

import org.eclipse.esmf.aspectmodel.generator.GenerationConfig;

Expand Down Expand Up @@ -67,21 +66,20 @@ public record OpenApiSchemaGenerationConfig(
}

public ObjectNode queriesTemplate() {
return Optional.ofNullable( template )
.map( getter( "paths" ) )
.map( getter( QUERIES_TEMPLATE_PATH ) )
.orElse( null );
if ( template == null ) {
return null;
}

final ObjectNode objectNode = getter( "paths" ).apply( template );
return getter( QUERIES_TEMPLATE_PATH ).apply( objectNode );
}

public ObjectNode documentTemplate() {
return Optional.ofNullable( template )
.map( ObjectNode::deepCopy )
.map( doc -> {
Optional.of( doc ).map( getter( "paths" ) ).ifPresent(
paths -> paths.remove( QUERIES_TEMPLATE_PATH )
);
return doc;
} )
.orElse( null );
if ( template == null ) {
return null;
}
final ObjectNode objectNode = template.deepCopy();
getter( "paths" ).apply( objectNode ).remove( QUERIES_TEMPLATE_PATH );
return objectNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,28 @@ void testValidParameter() throws IOException {
} );
}

@Test
void testValidTemplate() throws IOException {
final Aspect aspect = TestResources.load( TestAspect.ASPECT_WITHOUT_SEE_ATTRIBUTE ).aspect();
final OpenApiSchemaGenerationConfig config = OpenApiSchemaGenerationConfigBuilder.builder()
.useSemanticVersion( true )
.baseUrl( TEST_BASE_URL )
.resourcePath( TEST_RESOURCE_PATH )
.template( getTemplateParameter() )
.build();
final JsonNode json = apiJsonGenerator.apply( aspect, config ).getContent();
final SwaggerParseResult result = new OpenAPIParser().readContents( json.toString(), null, null );
assertThat( result.getMessages() ).isEmpty();

final OpenAPI openApi = result.getOpenAPI();
assertThat( openApi.getPaths().values().stream().findFirst().get().getGet().getResponses().get( "400" ).get$ref() )
.isEqualTo( "./core-api.yaml#/components/responses/ClientError" );
assertThat( openApi.getPaths().values().stream().findFirst().get().getGet().getResponses().get( "401" ).get$ref() )
.isEqualTo( "./core-api.yaml#/components/responses/Unauthorized" );
assertThat( openApi.getPaths().values().stream().findFirst().get().getGet().getResponses().get( "403" ).get$ref() )
.isEqualTo( "./core-api.yaml#/components/responses/Forbidden" );
}

@Test
void testInValidParameterName() throws IOException {
final ListAppender<ILoggingEvent> logAppender = new ListAppender<>();
Expand Down Expand Up @@ -495,11 +517,11 @@ void testAspectWithOperationWithSeeAttribute() {
final SwaggerParseResult result = new OpenAPIParser().readContents( json.toString(), null, null );
final OpenAPI openApi = result.getOpenAPI();
assertThat(
((Schema) openApi.getComponents().getSchemas().get( "testOperation" ).getAllOf()
.get( 1 )).getProperties() ).doesNotContainKey(
( (Schema) openApi.getComponents().getSchemas().get( "testOperation" ).getAllOf()
.get( 1 ) ).getProperties() ).doesNotContainKey(
"params" );
assertThat( ((Schema) openApi.getComponents().getSchemas().get( "testOperationTwo" ).getAllOf()
.get( 1 )).getProperties() ).doesNotContainKey( "params" );
assertThat( ( (Schema) openApi.getComponents().getSchemas().get( "testOperationTwo" ).getAllOf()
.get( 1 ) ).getProperties() ).doesNotContainKey( "params" );
}

@Test
Expand Down Expand Up @@ -635,8 +657,8 @@ void testAspectWithCommentForSeeAttributes() {
assertThat( openApi.getSpecVersion() ).isEqualTo( SpecVersion.V31 );
assertThat( openApi.getComponents().getSchemas().get( "AspectWithCollection" ).get$comment() ).isEqualTo(
"See: http://example.com/" );
assertThat( ((Schema) openApi.getComponents().getSchemas().get( "AspectWithCollection" ).getProperties()
.get( "testProperty" )).get$comment() )
assertThat( ( (Schema) openApi.getComponents().getSchemas().get( "AspectWithCollection" ).getProperties()
.get( "testProperty" ) ).get$comment() )
.isEqualTo( "See: http://example.com/, http://example.com/me" );
assertThat( openApi.getComponents().getSchemas().get( "TestCollection" ).get$comment() )
.isEqualTo( "See: http://example.com/" );
Expand Down Expand Up @@ -766,4 +788,10 @@ private ObjectNode getTestParameter() throws IOException {
final String inputString = IOUtils.toString( inputStream, StandardCharsets.UTF_8 );
return (ObjectNode) OBJECT_MAPPER.readTree( inputString );
}

private ObjectNode getTemplateParameter() throws IOException {
final InputStream inputStream = getClass().getResourceAsStream( "/openapi/open-api-error-template.yaml" );
final String inputString = IOUtils.toString( inputStream, StandardCharsets.UTF_8 );
return (ObjectNode) new YAMLMapper().readTree( inputString );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
paths:
__DEFAULT_QUERIES_TEMPLATE__:
get:
responses:
'400':
$ref: "core-api.yaml#/components/responses/ClientError"
'401':
$ref: "core-api.yaml#/components/responses/Unauthorized"
'403':
$ref: "core-api.yaml#/components/responses/Forbidden"

0 comments on commit 252bc6c

Please sign in to comment.