Skip to content
Permalink
Browse files
[GERONIMO-6775] ensure json examples are properly parsed in openapi
  • Loading branch information
rmannibucau committed Jun 26, 2020
1 parent b9de251 commit 1300b125168f8d3a469f6bd5709402ff16327c8d
Showing 2 changed files with 17 additions and 1 deletion.
@@ -18,11 +18,13 @@

import static java.beans.Introspector.decapitalize;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;
import static java.util.Optional.of;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

import java.io.StringReader;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@@ -42,9 +44,12 @@
import java.util.function.Supplier;
import java.util.stream.Stream;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonReaderFactory;
import javax.json.JsonString;
import javax.json.JsonValue;
import javax.json.bind.annotation.JsonbProperty;
@@ -61,6 +66,7 @@ public class SchemaProcessor {
private final Map<Type, org.eclipse.microprofile.openapi.models.media.Schema> cache = new HashMap<>();
private final Map<Class<?>, String> providedRefs = new HashMap<>();
private final Class<?> persistenceCapable;
private final JsonReaderFactory jsonReaderFactory;

public SchemaProcessor() {
Class<?> pc = null;
@@ -70,6 +76,7 @@ public SchemaProcessor() {
} catch (final NoClassDefFoundError | ClassNotFoundException e) {
// no-op
}
jsonReaderFactory = Json.createReaderFactory(emptyMap());
persistenceCapable = pc;
}

@@ -381,6 +388,15 @@ private void mergeSchema(final Supplier<Components> components,
impl.example(Integer.parseInt(example));
} else if (type == boolean.class || type == Boolean.class) {
impl.example(Boolean.parseBoolean(example));
} else if (type == String.class) {
impl.example(example);
} else if ((example.startsWith("{") && example.endsWith("}")) ||
(example.startsWith("[") && example.endsWith("}"))) {
try (final JsonReader reader = jsonReaderFactory.createReader(new StringReader(example))) {
impl.example(reader.readValue());
} catch (final Exception e) {
impl.example(example);
}
} else {
impl.example(example);
}
@@ -45,7 +45,7 @@ public void primitiveExample() {
assertEquals(Schema.SchemaType.OBJECT, schema.getType());
assertEquals("" +
"d=1.5 (java.lang.Double)\n" +
"data={\"name\":\"ok\"} (java.lang.String)\n" +
"data={\"name\":\"ok\"} (org.apache.johnzon.core.JsonObjectImpl)\n" +
"i=1 (java.lang.Integer)\n" +
"l=2 (java.lang.Integer)\n" +
"str=ok (java.lang.String)\n" +

0 comments on commit 1300b12

Please sign in to comment.