GERONIMO-6677 handling enums
authorRomain Manni-Bucau <rmannibucau@gmail.com>
Wed, 9 Jan 2019 13:05:56 +0000 (14:05 +0100)
committerRomain Manni-Bucau <rmannibucau@gmail.com>
Wed, 9 Jan 2019 13:05:56 +0000 (14:05 +0100)
geronimo-openapi-impl/pom.xml
geronimo-openapi-impl/src/main/java/org/apache/geronimo/microprofile/openapi/impl/processor/SchemaProcessor.java
geronimo-openapi-impl/src/test/java/org/apache/geronimo/microprofile/openapi/impl/processor/SchemaProcessorTest.java
geronimo-openapi-maven-plugin/pom.xml
pom.xml

index a650444..1b95f16 100644 (file)
@@ -19,7 +19,7 @@
   <parent>
     <artifactId>geronimo-openapi</artifactId>
     <groupId>org.apache.geronimo</groupId>
-    <version>1.0.5-SNAPSHOT</version>
+    <version>1.0.4-SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
 
index 1e5a295..9442e11 100644 (file)
@@ -16,6 +16,7 @@
  */
 package org.apache.geronimo.microprofile.openapi.impl.processor;
 
+import static java.util.Arrays.asList;
 import static java.util.Optional.of;
 import static java.util.Optional.ofNullable;
 import static java.util.stream.Collectors.toList;
@@ -83,23 +84,32 @@ public class SchemaProcessor {
                 schema.type(org.eclipse.microprofile.openapi.models.media.Schema.SchemaType.STRING).nullable(true);
             } else {
                 final Class from = Class.class.cast(model);
-                ofNullable(from.getAnnotation(Schema.class)).ifPresent(s -> sets(components, Schema.class.cast(s), schema));
-                // schema.items(new SchemaImpl());
-                schema.properties(new HashMap<>());
-                Class<?> current = from;
-                // todo: use getters first then fields, for JSON-B the private only fields must be ignored
-                while (current != null && current != Object.class) {
-                    Stream.of(current.getDeclaredFields())
-                            .filter(f -> {
-                                final boolean explicit = f.isAnnotationPresent(Schema.class);
-                                return !explicit || !f.getAnnotation(Schema.class).hidden();
-                            })
-                            .peek(f -> handleRequired(schema, f))
-                            .forEach(f -> {
-                                final String fieldName = findFieldName(f);
-                                schema.getProperties().put(fieldName, mapField(components, f));
-                            });
-                    current = current.getSuperclass();
+                if (from.isEnum()) {
+                    schema.type(org.eclipse.microprofile.openapi.models.media.Schema.SchemaType.STRING)
+                          .enumeration(asList(from.getEnumConstants()))
+                          .nullable(true);
+                } else {
+                    ofNullable(from.getAnnotation(Schema.class)).ifPresent(
+                            s -> sets(components, Schema.class.cast(s), schema));
+                    // schema.items(new SchemaImpl());
+                    schema.properties(new HashMap<>());
+                    Class<?> current = from;
+                    // todo: use getters first then fields, for JSON-B the private only fields must be ignored
+                    while (current != null && current != Object.class) {
+                        Stream.of(current.getDeclaredFields())
+                              .filter(f -> {
+                                  final boolean explicit = f.isAnnotationPresent(Schema.class);
+                                  return !explicit || !f.getAnnotation(Schema.class)
+                                                        .hidden();
+                              })
+                              .peek(f -> handleRequired(schema, f))
+                              .forEach(f -> {
+                                  final String fieldName = findFieldName(f);
+                                  schema.getProperties()
+                                        .put(fieldName, mapField(components, f));
+                              });
+                        current = current.getSuperclass();
+                    }
                 }
             }
         } else {
index e6b617c..6835717 100644 (file)
@@ -16,6 +16,7 @@
  */
 package org.apache.geronimo.microprofile.openapi.impl.processor;
 
+import static java.util.Arrays.asList;
 import static org.testng.Assert.assertEquals;
 
 import javax.json.bind.annotation.JsonbProperty;
@@ -39,6 +40,23 @@ public class SchemaProcessorTest {
         assertEquals(Schema.SchemaType.STRING, schema.getProperties().get("foo").getType());
     }
 
+    @Test
+    public void mapEnum() {
+        final Schema schema = new SchemaProcessor().mapSchemaFromClass(new ComponentsImpl(), DataWithEnum.class);
+        assertEquals(1, schema.getProperties().size());
+        final Schema anEnum = schema.getProperties().get("anEnum");
+        assertEquals(Schema.SchemaType.STRING, anEnum.getType());
+        assertEquals(asList(AnEnum.A, AnEnum.B), anEnum.getEnumeration());
+    }
+
+    public enum AnEnum {
+        A, B;
+    }
+
+    public static class DataWithEnum {
+        private AnEnum anEnum;
+    }
+
     public static class Data {
         private String name;
     }
index 816ee4a..bdb80b4 100644 (file)
@@ -19,7 +19,7 @@
   <parent>
     <artifactId>geronimo-openapi</artifactId>
     <groupId>org.apache.geronimo</groupId>
-    <version>1.0.5-SNAPSHOT</version>
+    <version>1.0.4-SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
 
diff --git a/pom.xml b/pom.xml
index d22a128..e0d1b20 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -26,7 +26,7 @@
 
   <groupId>org.apache.geronimo</groupId>
   <artifactId>geronimo-openapi</artifactId>
-  <version>1.0.5-SNAPSHOT</version>
+  <version>1.0.4-SNAPSHOT</version>
   <name>Geronimo OpenAPI</name>
   <packaging>pom</packaging>
   <description>