Skip to content

Commit bbcf676

Browse files
committed
- implement dyamic display based on tags
- ref #3820
1 parent ebd88ef commit bbcf676

File tree

8 files changed

+286
-30
lines changed

8 files changed

+286
-30
lines changed

modules/jooby-openapi/src/main/java/io/jooby/internal/openapi/asciidoc/AsciiDocContext.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import io.pebbletemplates.pebble.extension.AbstractExtension;
3434
import io.pebbletemplates.pebble.extension.Filter;
3535
import io.pebbletemplates.pebble.extension.Function;
36-
import io.pebbletemplates.pebble.lexer.Syntax;
3736
import io.pebbletemplates.pebble.loader.ClasspathLoader;
3837
import io.pebbletemplates.pebble.loader.DelegatingLoader;
3938
import io.pebbletemplates.pebble.loader.FileLoader;
@@ -44,7 +43,6 @@
4443

4544
public class AsciiDocContext {
4645
public static final BiConsumer<String, Schema<?>> NOOP = (name, schema) -> {};
47-
public static final Schema EMPTY_SCHEMA = new Schema<>();
4846

4947
private ObjectMapper json;
5048

@@ -133,7 +131,7 @@ public Map<String, Object> getGlobalVariables() {
133131
"..."));
134132
// Routes
135133
var operations =
136-
context.openapi.getOperations().stream()
134+
Optional.of(context.openapi.getOperations()).orElse(List.of()).stream()
137135
.map(op -> new HttpRequest(context, op, Map.of()))
138136
.toList();
139137
// so we can print routes without calling function: routes() vs routes
@@ -142,7 +140,7 @@ public Map<String, Object> getGlobalVariables() {
142140

143141
// Tags
144142
var tags =
145-
context.openapi.getTags().stream()
143+
Optional.ofNullable(context.openapi.getTags()).orElse(List.of()).stream()
146144
.map(
147145
tag ->
148146
new TagExt(
@@ -236,7 +234,6 @@ public Map<String, Filter> getFilters() {
236234
.collect(Collectors.toMap(Enum::name, it -> wrapFilter(it.name(), it)));
237235
}
238236
})
239-
.syntax(new Syntax.Builder().setEnableNewLineTrimming(false).build())
240237
.build();
241238
}
242239

@@ -384,7 +381,7 @@ private Map<String, Object> traverse(
384381
SneakyThrows.Function2<Schema<?>, Schema<?>, String> valueMapper,
385382
BiConsumer<String, Schema<?>> consumer,
386383
BiConsumer<String, Schema<?>> inner) {
387-
if (schema == EMPTY_SCHEMA) {
384+
if (schema == null) {
388385
return Map.of();
389386
}
390387
var resolved = resolveSchema(schema);

modules/jooby-openapi/src/main/java/io/jooby/internal/openapi/asciidoc/HttpMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface HttpMessage {
2121
AsciiDocContext context();
2222

2323
default Schema<?> selectBody(Schema<?> body, String modifier) {
24-
if (body != AsciiDocContext.EMPTY_SCHEMA) {
24+
if (body != null) {
2525
return switch (modifier) {
2626
case "full" -> body;
2727
case "simple" -> context().reduceSchema(body);

modules/jooby-openapi/src/main/java/io/jooby/internal/openapi/asciidoc/HttpRequest.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,12 @@ public String getQueryString(Map<String, Object> filter) {
151151
return "";
152152
}
153153

154-
@SuppressWarnings("unchecked")
155154
private Schema<?> getBody(List<String> contentType) {
156155
var body =
157156
Optional.ofNullable(operation.getRequestBody())
158157
.map(it -> toSchema(it.getContent(), contentType))
159158
.map(context::resolveSchema)
160-
.orElse(AsciiDocContext.EMPTY_SCHEMA);
159+
.orElse(null);
161160

162161
return selectBody(body, options.getOrDefault("body", "full").toString());
163162
}
@@ -170,7 +169,7 @@ public Schema<?> getForm() {
170169
BiFunction<Schema<?>, Map.Entry<String, String>, Map.Entry<String, String>> formatter) {
171170
var output = ArrayListMultimap.<String, String>create();
172171
var form = getForm();
173-
if (form != AsciiDocContext.EMPTY_SCHEMA) {
172+
if (form != null) {
174173
traverseSchema(null, form, formatter, output::put);
175174
}
176175
return output;
@@ -232,23 +231,21 @@ public ParameterList getAllParameters() {
232231
var parameters = allParameters();
233232
var body = getForm();
234233
var bodyType = "form";
235-
if (body == AsciiDocContext.EMPTY_SCHEMA) {
234+
if (body == null) {
236235
body = getBody();
237236
bodyType = "body";
238237
}
239238
var paramType = bodyType;
240-
if (body != AsciiDocContext.EMPTY_SCHEMA) {
241-
context.traverseSchema(
242-
body,
243-
(propertyName, schema) -> {
244-
var p = new Parameter();
245-
p.setName(propertyName);
246-
p.setSchema(schema);
247-
p.setIn(paramType);
248-
p.setDescription(schema.getDescription());
249-
parameters.add(p);
250-
});
251-
}
239+
context.traverseSchema(
240+
body,
241+
(propertyName, schema) -> {
242+
var p = new Parameter();
243+
p.setName(propertyName);
244+
p.setSchema(schema);
245+
p.setIn(paramType);
246+
p.setDescription(schema.getDescription());
247+
parameters.add(p);
248+
});
252249
return new ParameterList(parameters, ParameterList.PARAM);
253250
}
254251

modules/jooby-openapi/src/main/java/io/jooby/internal/openapi/asciidoc/HttpResponse.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,11 @@ public StatusCode getStatusCode() {
104104
return StatusCode.valueOf(statusCode);
105105
}
106106

107-
@SuppressWarnings("unchecked")
108107
private Schema<?> getBody(ResponseExt response) {
109108
return Optional.ofNullable(response)
110109
.map(it -> toSchema(it.getContent(), List.of()))
111110
.map(context()::resolveSchema)
112-
.orElse(AsciiDocContext.EMPTY_SCHEMA);
111+
.orElse(null);
113112
}
114113

115114
@NonNull @Override

modules/jooby-openapi/src/main/java/io/jooby/internal/openapi/asciidoc/display/RequestToCurl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public String render(Map<String, Object> args) {
6060
});
6161
if (formUrlEncoded.isEmpty()) {
6262
var body = request.getBody();
63-
if (body != AsciiDocContext.EMPTY_SCHEMA) {
63+
if (body != null) {
6464
options.put("-d", "'" + context.toJson(context.schemaProperties(body), false) + "'");
6565
}
6666
} else {

modules/jooby-openapi/src/main/java/io/jooby/internal/openapi/asciidoc/display/RequestToHttp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public String render(Map<String, Object> options) {
3939
.append('\n');
4040
}
4141
var schema = request.getBody();
42-
if (schema != AsciiDocContext.EMPTY_SCHEMA) {
42+
if (schema != null) {
4343
sb.append(context.toJson(context.schemaProperties(schema), false)).append('\n');
4444
}
4545
return sb.append("----").toString();

modules/jooby-openapi/src/main/java/io/jooby/internal/openapi/asciidoc/display/ResponseToHttp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public String render(Map<String, Object> options) {
2828
sb.append(header.getName()).append(": ").append(value).append('\n');
2929
}
3030
var schema = response.getBody();
31-
if (schema != AsciiDocContext.EMPTY_SCHEMA) {
31+
if (schema != null) {
3232
sb.append(context.getJson().writeValueAsString(context.schemaProperties(schema)))
3333
.append('\n');
3434
}

0 commit comments

Comments
 (0)