Skip to content

Commit afcdad3

Browse files
committed
- fix sample data/schema properties for array of basic types/as well as basic types it self
- fix links for array, arraylike models
1 parent 3daa475 commit afcdad3

File tree

4 files changed

+64
-15
lines changed

4 files changed

+64
-15
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import io.pebbletemplates.pebble.loader.Loader;
4242
import io.pebbletemplates.pebble.template.EvaluationContext;
4343
import io.pebbletemplates.pebble.template.PebbleTemplate;
44+
import io.swagger.v3.oas.models.media.BooleanSchema;
45+
import io.swagger.v3.oas.models.media.NumberSchema;
4446
import io.swagger.v3.oas.models.media.Schema;
4547

4648
public class AsciiDocContext {
@@ -313,11 +315,26 @@ public Schema<?> resolveSchema(Schema<?> schema) {
313315
public Object schemaProperties(Schema<?> schema) {
314316
var resolved = resolveSchema(schema);
315317
if ("array".equals(resolved.getType())) {
318+
var items = resolveSchema(resolved.getItems());
319+
if (items.getName() == null) {
320+
return List.of(basicTypeSample(items));
321+
}
316322
return List.of(traverse(resolved.getItems(), NOOP));
317323
}
324+
if (resolved.getName() == null) {
325+
return basicTypeSample(resolved);
326+
}
318327
return traverse(schema, NOOP);
319328
}
320329

330+
private Object basicTypeSample(Schema<?> items) {
331+
return switch (items) {
332+
case NumberSchema s -> 0;
333+
case BooleanSchema s -> true;
334+
default -> schemaType(items);
335+
};
336+
}
337+
321338
@SuppressWarnings("rawtypes")
322339
public Schema<?> reduceSchema(Schema<?> schema) {
323340
var truncated = emptySchema(schema);
@@ -348,6 +365,7 @@ public Schema<?> emptySchema(Schema<?> schema) {
348365
var empty = new Schema<>();
349366
empty.setType(resolved.getType());
350367
empty.setName(resolved.getName());
368+
empty.setTypes(resolved.getTypes());
351369
return empty;
352370
}
353371

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,23 @@ public Object apply(
105105
};
106106
var asciidoc = AsciiDocContext.from(context);
107107
var resolved = asciidoc.resolveSchema(schema);
108-
var target = resolved;
109-
var prefix = "";
110-
var suffix = "";
111-
if (resolved.getItems() != null) {
112-
target = asciidoc.resolveSchema(resolved.getItems());
113-
prefix = Optional.ofNullable(resolved.getName()).orElse("") + "[";
114-
suffix = "]";
108+
if (resolved.getItems() == null) {
109+
if (resolved.getName() == null) {
110+
return resolved.getType();
111+
}
112+
return new SafeString("<<" + resolved.getName() + ">>");
113+
} else {
114+
var item = asciidoc.resolveSchema(resolved.getItems());
115+
if (item.getName() == null) {
116+
// primitives
117+
return new SafeString(item.getType() + "[]");
118+
} else {
119+
if ("array".equals(resolved.getType())) {
120+
return new SafeString("<<" + item.getName() + ">>[]");
121+
}
122+
return new SafeString(resolved.getName() + "[<<" + item.getName() + ">>]");
123+
}
115124
}
116-
if ("object".equals(target.getType())) {
117-
return new SafeString(prefix + "<<" + target.getName() + ">>" + suffix);
118-
}
119-
// no link for basic types
120-
return prefix + target.getName() + suffix;
121125
}
122126
},
123127
curl {

modules/jooby-openapi/src/test/java/issues/i3820/App3820b.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,12 @@ public class App3820b extends Jooby {
1818
List<String> strings = new ArrayList<>();
1919
return strings;
2020
});
21+
22+
get(
23+
"/string",
24+
ctx -> {
25+
String value = "";
26+
return value;
27+
});
2128
}
2229
}

modules/jooby-openapi/src/test/java/issues/i3820/PebbleSupportTest.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,14 +959,34 @@ public void link(OpenAPIExt openapi) throws IOException {
959959

960960
templates
961961
.evaluateThat("{{GET(\"/library/search\") | response | link }}")
962-
.isEqualTo("[<<Book>>]");
962+
.isEqualTo("<<Book>>[]");
963963
}
964964

965965
@OpenAPITest(value = App3820b.class)
966-
public void linkPrimitives(OpenAPIExt openapi) throws IOException {
966+
public void checkPrimitives(OpenAPIExt openapi) throws IOException {
967967
var templates = new PebbleTemplateSupport(CurrentDir.testClass(getClass(), "adoc"), openapi);
968968

969-
templates.evaluateThat("{{GET(\"/strings\") | response | link }}").isEqualTo("Page[<<Book>>]");
969+
templates.evaluateThat("{{GET(\"/strings\") | response | link }}").isEqualTo("string[]");
970+
971+
templates
972+
.evaluateThat("{{GET(\"/strings\") | response | json }}")
973+
.isEqualToIgnoringNewLines(
974+
"""
975+
[source, json]
976+
----
977+
[ "string" ]
978+
----\
979+
""");
980+
981+
templates
982+
.evaluateThat("{{GET(\"/string\") | response | json }}")
983+
.isEqualToIgnoringNewLines(
984+
"""
985+
[source, json]
986+
----
987+
"string"
988+
----\
989+
""");
970990
}
971991

972992
@OpenAPITest(value = AppLib.class)

0 commit comments

Comments
 (0)