Skip to content

Commit 9188ae9

Browse files
authored
openapi.json - add reading operationId from swagger @operation annotation (#573)
1 parent b20e089 commit 9188ae9

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

http-generator-core/src/main/java/io/avaje/http/generator/core/MethodReader.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import io.avaje.http.generator.core.javadoc.Javadoc;
2424
import io.avaje.http.generator.core.openapi.MethodDocBuilder;
25+
import io.swagger.v3.oas.models.Operation;
2526

2627
public class MethodReader {
2728

@@ -269,6 +270,25 @@ private List<OpenAPIResponsePrism> buildApiResponses() {
269270
return responses;
270271
}
271272

273+
public void readOperation(Operation operation, Javadoc javadoc) {
274+
OperationPrism.getOptionalOn(element).ifPresent(an -> {
275+
operation.setOperationId(emptyToNull(an.operationId()));
276+
operation.setDeprecated(an.deprecated());
277+
operation.setSummary(emptyToNull(an.summary()));
278+
operation.setDescription(emptyToNull(an.description()));
279+
});
280+
if (operation.getDescription() == null) {
281+
operation.setDescription(javadoc.getDescription());
282+
}
283+
if (operation.getSummary() == null) {
284+
operation.setSummary(javadoc.getSummary());
285+
}
286+
}
287+
288+
private static String emptyToNull(String val) {
289+
return val.isEmpty() ? null : val;
290+
}
291+
272292
public <A> Optional<A> findAnnotation(Function<Element, Optional<A>> prismFunc) {
273293
return findAnnotation(prismFunc, element);
274294
}

http-generator-core/src/main/java/io/avaje/http/generator/core/openapi/MethodDocBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ public void build() {
3939
return;
4040
}
4141

42-
//operation.setOperationId();
43-
operation.setSummary(javadoc.getSummary());
44-
operation.setDescription(javadoc.getDescription());
42+
methodReader.readOperation(operation, javadoc);
4543
operation.setTags(methodReader.tags());
4644

4745
if (javadoc.isDeprecated()

http-generator-core/src/main/java/io/avaje/http/generator/core/package-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
@GeneratePrism(value = io.avaje.http.api.InstrumentServerContext.class)
2424
@GeneratePrism(value = io.avaje.http.api.ExceptionHandler.class)
2525
@GeneratePrism(value = io.swagger.v3.oas.annotations.OpenAPIDefinition.class, publicAccess = true)
26+
@GeneratePrism(value = io.swagger.v3.oas.annotations.Operation.class, publicAccess = true)
2627
@GeneratePrism(value = io.swagger.v3.oas.annotations.tags.Tag.class, publicAccess = true)
2728
@GeneratePrism(value = io.swagger.v3.oas.annotations.tags.Tags.class, publicAccess = true)
2829
@GeneratePrism(value = io.swagger.v3.oas.annotations.security.SecurityScheme.class, publicAccess = true)

tests/test-javalin/src/main/java/org/example/myapp/web/HelloController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.concurrent.CompletableFuture;
99
import java.util.concurrent.Executors;
1010

11+
import io.swagger.v3.oas.annotations.Operation;
1112
import org.example.myapp.service.MyService;
1213

1314
import io.avaje.http.api.BeanParam;
@@ -61,6 +62,7 @@ String getPlainMessage() {
6162
*/
6263
@Deprecated
6364
@Roles({AppRoles.ADMIN, AppRoles.BASIC_USER})
65+
@Operation(operationId = "helloByDate")
6466
@Get("/{id}/{date}")
6567
HelloDto hello(int id, LocalDate date, String otherParam) {
6668
return new HelloDto(id, date.toString(), otherParam);

tests/test-javalin/src/main/resources/public/openapi.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@
750750
],
751751
"summary" : "Return the Hello DTO",
752752
"description" : "",
753+
"operationId" : "helloByDate",
753754
"parameters" : [
754755
{
755756
"name" : "id",

0 commit comments

Comments
 (0)