Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customization of error responses in open api generator #638

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 @@ -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"
Loading