Skip to content
Permalink
Browse files
GERONIMO-6775 ensure primitive examples are coerced properly
  • Loading branch information
rmannibucau committed Jun 26, 2020
1 parent 84aaade commit b9de251c4ed37b3bf693215236245e31d225548b
Showing 3 changed files with 80 additions and 21 deletions.
@@ -171,7 +171,7 @@ private OpenAPI createOpenApi(final Class<?> application, final Stream<Class<?>>

// 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();
@@ -287,7 +287,7 @@ private org.eclipse.microprofile.openapi.models.media.Schema mapField(final Supp
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 Optional<String> findSchemaName(final AnnotatedElement m) {

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 @@ private void mergeSchema(final Supplier<Components> components,
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 @@ private void sets(final Supplier<Components> components, final Schema schema,
fillSchema(components, schema.implementation(), impl, providedRef);
}
}
mergeSchema(components, impl, schema);
mergeSchema(components, impl, schema, null);
}
}

@@ -16,27 +16,49 @@
*/
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 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 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 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 -> {
@@ -86,7 +108,7 @@ public void cyclicRef() {
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 @@ public static class SomeTypeField {
protected Type type;
}

public static class SomeClassWithTwoIdenticalObjects{
public static class SomeClassWithTwoIdenticalObjects {
protected SomeTypeField type;
protected SomeTypeField anotherType;
}
@@ -225,4 +247,24 @@ public static class JsonbData {
@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;
}
}

0 comments on commit b9de251

Please sign in to comment.