Skip to content

Commit

Permalink
[GDC-161] openapi doc strings with literal block style (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaria committed Dec 21, 2023
1 parent 647f314 commit 3fe8f71
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ openapi: 3.0.2
info:
title: Breweries
version: 3.0.0
description: |
This is an API for brewery data.
It tells you all about breweries and their beers.
And much much more.
paths:
/noop:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.dotwebstack.framework.core.helpers.ExceptionHelper.invalidConfigurationException;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -21,7 +22,7 @@ public class OpenApiRequestHandler implements HandlerFunction<ServerResponse> {

public OpenApiRequestHandler(InputStream openApiStream) {
try {
var mapper = new YAMLMapper();
var mapper = new YAMLMapper().enable(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE);
ObjectNode specNode = VendorExtensionHelper.removeVendorExtensions(openApiStream, mapper);
openApiSpec = mapper.writer()
.writeValueAsString(specNode);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.dotwebstack.framework.service.openapi.handler;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;

import java.util.List;
import lombok.NonNull;
import org.dotwebstack.framework.service.openapi.TestResources;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.web.reactive.function.server.HandlerStrategies;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.result.view.ViewResolver;
import reactor.test.StepVerifier;


class OpenApiRequestHandlerTest {

private ServerResponse.Context context;

@BeforeEach
public void createContext() {
HandlerStrategies strategies = HandlerStrategies.withDefaults();
context = new ServerResponse.Context() {

@NonNull
@Override
public List<HttpMessageWriter<?>> messageWriters() {
return strategies.messageWriters();
}

@NonNull
@Override
public List<ViewResolver> viewResolvers() {
return strategies.viewResolvers();
}
};
}

@Test
void handle_returnsExpectedServerResponse_whenRequested() {
var serverHttpRequest = MockServerHttpRequest.method(HttpMethod.GET, "")
.build();
var exchange = MockServerWebExchange.from(serverHttpRequest);
var request = ServerRequest.create(exchange, List.of());

var responseMono = new OpenApiRequestHandler(TestResources.openApiStream()).handle(request)
.flatMap(serverResponse -> {
assertThat(serverResponse.statusCode(), is(HttpStatus.OK));
assertThat(serverResponse.headers()
.get("Content-type"), hasItem("text/yaml;charset=utf-8"));
return serverResponse.writeTo(exchange, context);
});

StepVerifier.create(responseMono)
.verifyComplete();

StepVerifier.create(exchange.getResponse()
.getBodyAsString())
.assertNext(body -> {
assertThat(body, containsString("openapi: \"3.0.2\""));
assertThat(body, containsString("""
description: |
This is an API for brewery data.
It tells you all about breweries and their beers.
And much much more.
"""));
})
.verifyComplete();
}
}
4 changes: 4 additions & 0 deletions service/openapi/src/test/resources/config/model/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ openapi: 3.0.2
info:
title: test API
version: 3.0.0
description: |
This is an API for brewery data.
It tells you all about breweries and their beers.
And much much more.
components:
schemas:
Expand Down

0 comments on commit 3fe8f71

Please sign in to comment.