Skip to content

BeanParam OpenAPI #179

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 4 commits into from
Mar 10, 2023
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
@@ -1,7 +1,8 @@
package io.avaje.http.generator.core;

import static io.avaje.http.generator.core.ProcessingContext.*;
import static io.avaje.http.generator.core.ParamType.RESPONSE_HANDLER;
import static io.avaje.http.generator.core.ProcessingContext.platform;
import static io.avaje.http.generator.core.ProcessingContext.typeElement;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -245,7 +246,7 @@ void writeParamName(Append writer) {
* Build the OpenAPI documentation for this parameter.
*/
void buildApiDocumentation(MethodDocBuilder methodDoc) {
if (!isPlatformContext() && !isParamMap) {
if (!isPlatformContext() && !isParamMap && paramType != ParamType.BEANPARAM) {
new MethodParamDocBuilder(methodDoc, this).build();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.avaje.http.generator.core;

import io.avaje.http.generator.core.openapi.MethodDocBuilder;
import static io.avaje.http.generator.core.ProcessingContext.asElement;

import javax.lang.model.element.ElementKind;
import javax.lang.model.element.VariableElement;

import io.avaje.http.generator.core.openapi.MethodDocBuilder;

public class MethodParam {

private final ElementReader elementParam;
Expand All @@ -29,7 +32,25 @@ public void buildParamName(Append writer) {
}

public void buildApiDocumentation(MethodDocBuilder methodDoc) {
elementParam.buildApiDocumentation(methodDoc);
if (elementParam.paramType() != ParamType.BEANPARAM)
elementParam.buildApiDocumentation(methodDoc);
else {
asElement(elementParam.element().asType()).getEnclosedElements().stream()
.filter(e -> e.getKind() == ElementKind.FIELD)
.map(VariableElement.class::cast)
.forEach(
e -> {
final var typeMirror = e.asType();

new ElementReader(
e,
Util.parse(typeMirror.toString()),
Util.typeDef(typeMirror),
ParamType.QUERYPARAM,
false)
.buildApiDocumentation(methodDoc);
});
}
}

public boolean isBody() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.avaje.http.generator.core.SecuritySchemesPrism;
import io.avaje.http.generator.core.TagPrism;
import io.avaje.http.generator.core.TagsPrism;
import io.avaje.http.generator.core.Util;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand Down Expand Up @@ -77,12 +78,17 @@ private OpenAPI initOpenAPI() {
}

Schema toSchema(String rawType, Element element) {
TypeElement typeElement = elements.getTypeElement(rawType);
final var typeElement = elements.getTypeElement(rawType);
final var varElement =
elements.getTypeElement(Util.trimAnnotations(element.asType().toString()));

if (typeElement == null) {
// primitive types etc
return schemaBuilder.toSchema(element.asType());
} else if (varElement != null) {
return schemaBuilder.toSchema(element);
} else {
return schemaBuilder.toSchema(typeElement.asType());
return schemaBuilder.toSchema(typeElement);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ private static TypeMirror typeArgument(TypeMirror type) {
return typeArguments.get(0);
}

Schema<?> toSchema(Element element) {
var schema = toSchema(element.asType());

setLengthMinMax(element, schema);
setFormatFromValidation(element, schema);
if (isNotNullable(element)) {
schema.setNullable(Boolean.FALSE);
}
return schema;
}

Schema<?> toSchema(TypeMirror type) {
if (types.isAssignable(type, completableFutureType)) {
type = typeArgument(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import io.avaje.http.api.Header;
import io.avaje.jsonb.Json;

@Json
Expand All @@ -22,6 +23,8 @@ public class GetBeanForm {
private String email;

private List<String> addresses;
@Header
private String head;

public String getName() {
return name;
Expand Down Expand Up @@ -56,4 +59,12 @@ public List<String> getAddresses() {
public void setAddresses(List<String> addresses) {
this.addresses = addresses;
}

public String getHead() {
return head;
}

public void setHead(String head) {
this.head = head;
}
}
125 changes: 100 additions & 25 deletions tests/test-javalin-jsonb/src/main/resources/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,39 @@
"description" : "",
"parameters" : [
{
"name" : "bean",
"in" : "bean",
"name" : "name",
"in" : "query",
"schema" : {
"maxLength" : 150,
"minLength" : 2,
"type" : "string",
"nullable" : false
}
},
{
"name" : "email",
"in" : "query",
"schema" : {
"$ref" : "#/components/schemas/GetBeanForm"
"maxLength" : 100,
"type" : "string",
"format" : "email"
}
},
{
"name" : "addresses",
"in" : "query",
"schema" : {
"type" : "array",
"items" : {
"type" : "string"
}
}
},
{
"name" : "head",
"in" : "header",
"schema" : {
"type" : "string"
}
}
],
Expand Down Expand Up @@ -1125,6 +1154,40 @@
}
}
},
"/test/byteArray" : {
"get" : {
"tags" : [

],
"summary" : "",
"description" : "",
"requestBody" : {
"content" : {
"application/json" : {
"schema" : {
"type" : "array",
"items" : {
"$ref" : "#/components/schemas/byte"
}
}
}
},
"required" : true
},
"responses" : {
"200" : {
"description" : "",
"content" : {
"application/json" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
},
"/test/ctx" : {
"get" : {
"tags" : [
Expand Down Expand Up @@ -1441,6 +1504,37 @@
}
}
},
"/test/inputStream" : {
"get" : {
"tags" : [

],
"summary" : "",
"description" : "",
"requestBody" : {
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/InputStream"
}
}
},
"required" : true
},
"responses" : {
"200" : {
"description" : "",
"content" : {
"application/json" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
},
"/test/int" : {
"put" : {
"tags" : [
Expand Down Expand Up @@ -1805,28 +1899,6 @@
}
}
},
"GetBeanForm" : {
"type" : "object",
"properties" : {
"name" : {
"maxLength" : 150,
"minLength" : 2,
"type" : "string",
"nullable" : false
},
"email" : {
"maxLength" : 100,
"type" : "string",
"format" : "email"
},
"addresses" : {
"type" : "array",
"items" : {
"type" : "string"
}
}
}
},
"HelloDto" : {
"type" : "object",
"properties" : {
Expand Down Expand Up @@ -1876,6 +1948,9 @@
}
}
},
"InputStream" : {
"type" : "object"
},
"MyForm" : {
"type" : "object",
"properties" : {
Expand Down