GERONIMO-6775 ensure primitive examples are coerced properly
diff --git a/geronimo-openapi-impl/src/main/java/org/apache/geronimo/microprofile/openapi/cdi/GeronimoOpenAPIExtension.java b/geronimo-openapi-impl/src/main/java/org/apache/geronimo/microprofile/openapi/cdi/GeronimoOpenAPIExtension.java
index bb2997c..32afde0 100644
--- a/geronimo-openapi-impl/src/main/java/org/apache/geronimo/microprofile/openapi/cdi/GeronimoOpenAPIExtension.java
+++ b/geronimo-openapi-impl/src/main/java/org/apache/geronimo/microprofile/openapi/cdi/GeronimoOpenAPIExtension.java
@@ -171,7 +171,7 @@
// adds the context path to the base
final Instance<ServletContext> servletContextInstance = current.select(ServletContext.class);
- final boolean appendContextPath = Boolean.valueOf(config.read("application.append-context-path", "true"));
+ final boolean appendContextPath = Boolean.parseBoolean(config.read("application.append-context-path", "true"));
String contextPath = "";
if (appendContextPath && !servletContextInstance.isAmbiguous() && !servletContextInstance.isUnsatisfied()) {
contextPath = servletContextInstance.get().getContextPath();
diff --git a/geronimo-openapi-impl/src/main/java/org/apache/geronimo/microprofile/openapi/impl/processor/SchemaProcessor.java b/geronimo-openapi-impl/src/main/java/org/apache/geronimo/microprofile/openapi/impl/processor/SchemaProcessor.java
index 19d1938..5e78295 100644
--- a/geronimo-openapi-impl/src/main/java/org/apache/geronimo/microprofile/openapi/impl/processor/SchemaProcessor.java
+++ b/geronimo-openapi-impl/src/main/java/org/apache/geronimo/microprofile/openapi/impl/processor/SchemaProcessor.java
@@ -287,7 +287,7 @@
final org.eclipse.microprofile.openapi.models.media.Schema schemaFromClass = mapSchemaFromClass(
components, type);
if (annotation != null) {
- mergeSchema(components, schemaFromClass, annotation);
+ mergeSchema(components, schemaFromClass, annotation, type);
}
return schemaFromClass;
});
@@ -349,7 +349,7 @@
private void mergeSchema(final Supplier<Components> components,
final org.eclipse.microprofile.openapi.models.media.Schema impl,
- final Schema schema) {
+ final Schema schema, final Type type) {
if (schema.deprecated()) {
impl.deprecated(schema.deprecated());
}
@@ -368,8 +368,25 @@
if (!schema.ref().isEmpty()) {
impl.ref(schema.ref());
}
- if (!schema.example().isEmpty()) {
- impl.example(schema.example());
+ final String example = schema.example();
+ if (!example.isEmpty()) {
+ if (type != null) {
+ if (type == double.class || type == Double.class ||
+ type == float.class || type == Float.class) {
+ impl.example(Double.parseDouble(example));
+ } else if (type == long.class || type == Long.class ||
+ type == int.class || type == Integer.class ||
+ type == short.class || type == Short.class ||
+ type == byte.class || type == Byte.class) {
+ impl.example(Integer.parseInt(example));
+ } else if (type == boolean.class || type == Boolean.class) {
+ impl.example(Boolean.parseBoolean(example));
+ } else {
+ impl.example(example);
+ }
+ } else {
+ impl.example(example);
+ }
}
of(schema.not()).filter(it -> it != Void.class).ifPresent(t -> impl.not(mapSchemaFromClass(components, t)));
final List<org.eclipse.microprofile.openapi.models.media.Schema> oneOf = Stream.of(schema.oneOf())
@@ -478,7 +495,7 @@
fillSchema(components, schema.implementation(), impl, providedRef);
}
}
- mergeSchema(components, impl, schema);
+ mergeSchema(components, impl, schema, null);
}
}
diff --git a/geronimo-openapi-impl/src/test/java/org/apache/geronimo/microprofile/openapi/impl/processor/SchemaProcessorTest.java b/geronimo-openapi-impl/src/test/java/org/apache/geronimo/microprofile/openapi/impl/processor/SchemaProcessorTest.java
index b19a080..92c184a 100644
--- a/geronimo-openapi-impl/src/test/java/org/apache/geronimo/microprofile/openapi/impl/processor/SchemaProcessorTest.java
+++ b/geronimo-openapi-impl/src/test/java/org/apache/geronimo/microprofile/openapi/impl/processor/SchemaProcessorTest.java
@@ -16,28 +16,50 @@
*/
package org.apache.geronimo.microprofile.openapi.impl.processor;
-import static java.util.Arrays.asList;
-import static java.util.stream.Collectors.toSet;
-import static org.testng.Assert.assertEquals;
-
-import java.lang.reflect.Type;
-import java.util.List;
-import java.util.function.Supplier;
-import java.util.stream.Stream;
-
-import javax.json.JsonArray;
-import javax.json.JsonObject;
-import javax.json.JsonValue;
-import javax.json.bind.annotation.JsonbProperty;
-
import org.apache.geronimo.microprofile.openapi.impl.model.ComponentsImpl;
import org.apache.geronimo.microprofile.openapi.openjpa.Entity1;
import org.eclipse.microprofile.openapi.models.Components;
import org.eclipse.microprofile.openapi.models.media.Schema;
import org.testng.annotations.Test;
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.json.JsonValue;
+import javax.json.bind.annotation.JsonbProperty;
+import java.lang.reflect.Type;
+import java.util.List;
+import java.util.function.Supplier;
+import java.util.stream.Stream;
+
+import static java.util.Arrays.asList;
+import static java.util.stream.Collectors.joining;
+import static java.util.stream.Collectors.toSet;
+import static org.testng.Assert.assertEquals;
+
public class SchemaProcessorTest {
@Test
+ public void primitiveExample() {
+ final Supplier<Components> components = newComponentsProvider();
+ final Schema tmp = new SchemaProcessor().mapSchemaFromClass(components, WithPrimitives.class);
+ final Schema schema = components.get().getSchemas().get(tmp.getRef().substring("#/components/schemas/".length()));
+ assertEquals(Schema.SchemaType.OBJECT, schema.getType());
+ assertEquals("" +
+ "d=1.5 (java.lang.Double)\n" +
+ "data={\"name\":\"ok\"} (java.lang.String)\n" +
+ "i=1 (java.lang.Integer)\n" +
+ "l=2 (java.lang.Integer)\n" +
+ "str=ok (java.lang.String)\n" +
+ "yes=true (java.lang.Boolean)",
+ schema.getProperties().entrySet().stream()
+ .map(i -> {
+ final Object example = i.getValue().getExample();
+ return i.getKey() + "=" + example + " (" + example.getClass().getName() + ")";
+ })
+ .sorted()
+ .collect(joining("\n")));
+ }
+
+ @Test
public void mapJsonp() {
Stream.of(JsonValue.class, JsonObject.class).forEach(it -> {
final Schema schema = new SchemaProcessor().mapSchemaFromClass(newComponentsProvider(), JsonValue.class);
@@ -86,7 +108,7 @@
assertSomeRelatedClass(children.getItems());
assertEquals(2, components.getSchemas().size());
final Schema completeSchema =
- components.getSchemas().get("org_apache_geronimo_microprofile_openapi_impl_processor_SchemaProcessorTest_SomeClass");
+ components.getSchemas().get("org_apache_geronimo_microprofile_openapi_impl_processor_SchemaProcessorTest_SomeClass");
assertEquals(3, completeSchema.getProperties().size());
assertEquals(Stream.of("simple", "child", "children").collect(toSet()), completeSchema.getProperties().keySet());
}
@@ -189,7 +211,7 @@
protected Type type;
}
- public static class SomeClassWithTwoIdenticalObjects{
+ public static class SomeClassWithTwoIdenticalObjects {
protected SomeTypeField type;
protected SomeTypeField anotherType;
}
@@ -225,4 +247,24 @@
@JsonbProperty("foo")
protected String name;
}
+
+ public static class WithPrimitives {
+ @org.eclipse.microprofile.openapi.annotations.media.Schema(example = "{\"name\":\"ok\"}")
+ protected Data data;
+
+ @org.eclipse.microprofile.openapi.annotations.media.Schema(example = "ok")
+ protected String str;
+
+ @org.eclipse.microprofile.openapi.annotations.media.Schema(example = "1")
+ protected int i;
+
+ @org.eclipse.microprofile.openapi.annotations.media.Schema(example = "2")
+ protected long l;
+
+ @org.eclipse.microprofile.openapi.annotations.media.Schema(example = "1.5")
+ protected double d;
+
+ @org.eclipse.microprofile.openapi.annotations.media.Schema(example = "true")
+ protected boolean yes;
+ }
}