Skip to content

Fix Helidon path openapi generation #596

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

Merged
merged 3 commits into from
Apr 27, 2025
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 @@ -81,7 +81,7 @@ private String requestMedia(Schema<?> schema) {
if (schema instanceof StringSchema) {
return APP_TXT;
}
boolean asForm = (paramType == ParamType.FORM);
boolean asForm = paramType == ParamType.FORM;
return asForm ? APP_FORM : APP_JSON;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ private void writeAddRoutes() {
writeRoutes(methods);
for (final ControllerMethodWriter methodWriter : methods) {
methodWriter.writeHandler(isRequestScoped());
if (!reader.isDocHidden()) {
methodWriter.buildApiDocumentation();
}
}
}

Expand All @@ -108,9 +111,6 @@ private void writeRoutes(List<ControllerMethodWriter> methods) {

for (final ControllerMethodWriter methodWriter : methods) {
methodWriter.writeRule();
if (!reader.isDocHidden()) {
methodWriter.buildApiDocumentation();
}
}
writer.append(" }").eol().eol();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import java.util.List;

import io.avaje.http.api.Controller;
import io.avaje.http.api.Delete;
import io.avaje.http.api.Get;
import io.avaje.http.api.Header;
import io.avaje.http.api.MediaType;
import io.avaje.http.api.OpenAPIResponse;
import io.avaje.http.api.OpenAPIResponses;
import io.avaje.http.api.Path;
import io.avaje.http.api.Post;
import io.avaje.http.api.Produces;
import io.avaje.http.api.Put;
import io.avaje.http.api.QueryParam;
import io.javalin.http.Context;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
Expand Down Expand Up @@ -93,10 +96,16 @@ Person testPostList(List<Person> m) {
@Produces(value = MediaType.TEXT_PLAIN, statusCode = 203)
@OpenAPIResponse(responseCode = 204, type = String.class)
String testDefaultStatus(Context ctx) {
if (ctx.contentType().equals(MediaType.APPLICATION_PDF)) {
if (MediaType.APPLICATION_PDF.equals(ctx.contentType())) {
ctx.status(204);
return "";
}
return "only partial info";
}

@Delete("/delete/{type}")
String testPathParam(String type, @QueryParam String lastName, @Header String header) {

return "only partial info";
}
}
57 changes: 51 additions & 6 deletions tests/test-javalin-jsonb/src/test/resources/expectedOpenApi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,64 @@
"description" : "Example Javalin controllers with Java and Maven",
"version" : ""
},
"servers" : [
{
"url" : "localhost:8080",
"description" : "local testing"
}
],
"servers" : [
{
"url" : "localhost:8080",
"description" : "local testing"
}
],
"tags" : [
{
"name" : "tag1",
"description" : "this is added to openapi tags"
}
],
"paths" : {
"/openapi/delete/{type}" : {
"delete" : {
"tags" : [

],
"summary" : "",
"description" : "",
"parameters" : [
{
"name" : "type",
"in" : "path",
"required" : true,
"schema" : {
"type" : "string"
}
},
{
"name" : "lastName",
"in" : "query",
"schema" : {
"type" : "string"
}
},
{
"name" : "header",
"in" : "header",
"schema" : {
"type" : "string"
}
}
],
"responses" : {
"200" : {
"description" : "",
"content" : {
"application/json" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
},
"/openapi/get" : {
"get" : {
"tags" : [
Expand Down
13 changes: 13 additions & 0 deletions tests/test-nima-jsonb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,24 @@

<properties>
<maven.compiler.release>21</maven.compiler.release>
<swagger.version>2.2.30</swagger.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<surefire.useModulePath>false</surefire.useModulePath>
</properties>

<dependencies>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>


<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
Expand Down
113 changes: 113 additions & 0 deletions tests/test-nima-jsonb/src/main/java/org/example/OpenAPIController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package org.example;

import java.util.List;

import io.avaje.http.api.Controller;
import io.avaje.http.api.Delete;
import io.avaje.http.api.Get;
import io.avaje.http.api.Header;
import io.avaje.http.api.MediaType;
import io.avaje.http.api.OpenAPIResponse;
import io.avaje.http.api.OpenAPIResponses;
import io.avaje.http.api.Path;
import io.avaje.http.api.Post;
import io.avaje.http.api.Produces;
import io.avaje.http.api.Put;
import io.avaje.http.api.QueryParam;
import io.helidon.webserver.http.ServerRequest;
import io.helidon.webserver.http.ServerResponse;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
import io.swagger.v3.oas.annotations.tags.Tag;

@OpenAPIDefinition(
info =
@Info(
title = "Example service",
description = "Example Helidon controllers with Java and Maven"))
@Controller
@Path("openapi/")
@SecurityScheme(
type = SecuritySchemeType.APIKEY,
in = SecuritySchemeIn.QUERY,
name = "JWT",
paramName = "access_token",
description =
"JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.")
public class OpenAPIController {

/**
* Example of Open API Get (up to the first period is the summary). When using Javalin Context
* only <br>
* This Javadoc description is added to the generated openapi.json
*
* @return funny phrase (this part of the javadoc is added to the response desc)
*/
@Get("/get")
@Produces(MediaType.TEXT_PLAIN)
@OpenAPIResponse(responseCode = 200, type = String.class)
void ctxEndpoint(ServerRequest req, ServerResponse res) {}

/**
* Standard Post. uses tag annotation to add tags to openapi json
*
* @param b the body (this is used for generated request body desc)
* @return the response body (from javadoc)
*/
@Post("/post")
@Tag(name = "tag1", description = "this is added to openapi tags")
@OpenAPIResponse(responseCode = 200, description = "overrides @return javadoc description")
@OpenAPIResponse(responseCode = 201)
@OpenAPIResponse(
responseCode = 400,
description = "User not found (Will not have an associated response schema)")
@OpenAPIResponse(
responseCode = 500,
description = "Some other Error (Will have this error class as the response class)",
type = ErrorResponse.class)
Person testPost(Person b) {
return new Person(0, "baby");
}

public static class ErrorResponse {

public String id;
public String text;
}

/**
* Standard Post. The Deprecated annotation adds "deprecacted:true" to the generated json
*
* @param m the body
* @return the response body (from javadoc)
*/
@Deprecated
@Post("/post1")
@OpenAPIResponses({
@OpenAPIResponse(responseCode = 400, description = "User not found"),
@OpenAPIResponse(
responseCode = 500,
description = "Some other Error",
type = ErrorResponse.class)
})
Person testPostList(List<Person> m) {
return new Person(0, "baby");
}

@Put("/put")
@Produces(value = MediaType.TEXT_PLAIN, statusCode = 203)
@OpenAPIResponse(responseCode = 204, type = String.class)
String testDefaultStatus() {

return "only partial info";
}

@Delete("/delete/{type}")
String testPathParam(String type, @QueryParam String lastName, @Header String header) {

return "only partial info";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.avaje.http.generator.helidon.nima.HelidonProcessor;

class NimaProcessorTest {
Expand Down Expand Up @@ -76,4 +78,37 @@ private Iterable<JavaFileObject> getSourceFiles(String source) throws Exception
final Set<Kind> fileKinds = Collections.singleton(Kind.SOURCE);
return files.list(StandardLocation.SOURCE_PATH, "", fileKinds, true);
}

@Test
public void testOpenAPIGeneration() throws Exception {
final var source = Paths.get("src").toAbsolutePath().toString();
// OpenAPIController
final var files = getSourceFiles(source);

Iterable<JavaFileObject> openAPIController = null;
for (final var file : files) {
if (file.isNameCompatible("OpenAPIController", Kind.SOURCE))
openAPIController = List.of(file);
}
final var compiler = ToolProvider.getSystemJavaCompiler();

final var task =
compiler.getTask(
new PrintWriter(System.out),
null,
null,
List.of("--release=21"),
null,
openAPIController);
task.setProcessors(List.of(new HelidonProcessor()));

assertThat(task.call()).isTrue();

final var mapper = new ObjectMapper();
final var expectedOpenApiJson =
mapper.readTree(new File("src/test/resources/expectedOpenApi.json"));
final var generatedOpenApi = mapper.readTree(new File("openapi.json"));

assert expectedOpenApiJson.equals(generatedOpenApi);
}
}
Loading