Skip to content

Commit 219b7a5

Browse files
bamthomasmvanzalu
andcommitted
feat: allows to return OpenAPI yaml format
Co-authored-by: Maxime Vanza Lutonda <mvanzalu@users.noreply.github.com>
1 parent 4c11347 commit 219b7a5

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

datashare-app/src/main/java/org/icij/datashare/web/OpenApiResource.java

+22-5
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,41 @@
33
import com.fasterxml.jackson.databind.node.ObjectNode;
44
import com.google.inject.Singleton;
55
import io.swagger.v3.core.util.Json31;
6+
import io.swagger.v3.core.util.Yaml31;
67
import io.swagger.v3.oas.annotations.Operation;
8+
import io.swagger.v3.oas.annotations.Parameter;
9+
import io.swagger.v3.oas.annotations.enums.ParameterIn;
710
import io.swagger.v3.oas.annotations.responses.ApiResponse;
811
import io.swagger.v3.oas.models.OpenAPI;
912
import net.codestory.http.annotations.Get;
1013
import net.codestory.http.annotations.Prefix;
14+
import net.codestory.http.errors.BadRequestException;
1115
import net.codestory.http.payload.Payload;
1216
import org.icij.swagger.FluentReader;
1317

18+
import static java.util.Optional.ofNullable;
1419
import static org.icij.swagger.ClassUtils.findAllClassesUsingClassLoader;
1520

1621
@Singleton
17-
@Prefix("/api/openapi")
22+
@Prefix("/api/openapi?format=:format")
1823
public class OpenApiResource {
19-
@Operation(description = "Get the JSON OpenAPI v3 contract specification")
20-
@ApiResponse(responseCode = "200", description="returns the JSON file")
24+
@Operation(description = "Get the JSON or YAML OpenAPI v3 contract specification",
25+
parameters = {
26+
@Parameter(name = "format",
27+
description = "format of openapi description. Possible values are \"json\" or \"yaml\". Default=\"json\".",
28+
in = ParameterIn.QUERY)
29+
}
30+
)
31+
@ApiResponse(responseCode = "200", description="returns the JSON or YAML file")
2132
@Get()
22-
public Payload get() {
33+
public Payload get(String format) {
2334
final OpenAPI openAPI = new FluentReader().read(findAllClassesUsingClassLoader(getClass().getPackageName()));
24-
return new Payload("text/json", Json31.mapper().convertValue(openAPI, ObjectNode.class).toString());
35+
if ("json".equals(ofNullable(format).orElse("json"))) {
36+
return new Payload("text/json", Json31.mapper().convertValue(openAPI, ObjectNode.class).toString());
37+
} else if ("yaml".equals(format)) {
38+
return new Payload("text/yaml", Yaml31.pretty(openAPI));
39+
} else {
40+
throw new BadRequestException();
41+
}
2542
}
2643
}

datashare-app/src/test/java/org/icij/datashare/web/OpenApiResourceTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ public void test_get() {
1212
haveType("text/json").
1313
contain("\"openapi\":\"3.0.1\"");
1414
}
15+
@Test
16+
public void test_get_yaml() {
17+
get("/api/openapi?format=yaml").should().respond(200).
18+
haveType("text/yaml").
19+
contain("openapi: 3.0.1");
20+
}
1521

1622
@Before
1723
public void setUp() {

0 commit comments

Comments
 (0)