diff --git a/jooby-raml/src/main/java/org/jooby/internal/raml/RamlBuilder.java b/jooby-raml/src/main/java/org/jooby/internal/raml/RamlBuilder.java index 258239266f..246781df66 100644 --- a/jooby-raml/src/main/java/org/jooby/internal/raml/RamlBuilder.java +++ b/jooby-raml/src/main/java/org/jooby/internal/raml/RamlBuilder.java @@ -272,7 +272,7 @@ public String build(final List routes) { Consumer typeCollector = type -> { if (type != Object.class && type != void.class) { RamlType.parseAll(type).stream() - .filter(t -> t.isObject() || t.isEnum()) + .filter(t -> t.isCustom()) .forEach(types::add); } }; diff --git a/jooby-raml/src/main/java/org/jooby/internal/raml/RamlType.java b/jooby-raml/src/main/java/org/jooby/internal/raml/RamlType.java index 63d6a2aecf..e4dbb95707 100644 --- a/jooby-raml/src/main/java/org/jooby/internal/raml/RamlType.java +++ b/jooby-raml/src/main/java/org/jooby/internal/raml/RamlType.java @@ -32,6 +32,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.UUID; import com.google.common.collect.ImmutableList; @@ -49,6 +50,8 @@ public class RamlType { private List values; + private String pattern; + public RamlType(final String type) { this.type = type; } @@ -65,6 +68,8 @@ public Map properties() { return properties; } + public String pattern(){ return pattern; } + @Override public boolean equals(final Object obj) { if (obj instanceof RamlType) { @@ -115,6 +120,9 @@ public String toString(int level) { if (values != null) { buff.append(indent(level)).append("enum: ").append(values.toString()).append("\n"); } + if (pattern != null) { + buff.append(indent(level)).append("pattern: ").append(pattern).append("\n"); + } buff.setLength(buff.length() - 1); return buff.toString(); } @@ -196,6 +204,10 @@ private static RamlType simpleParse(final Type type) { enums.add(((Enum) value).name()); } complex.values = enums; + } else if (UUID.class.isAssignableFrom(rawType)) { + complex = new RamlType("string"); + complex.name = "uuid"; + complex.pattern = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"; } else { complex = new RamlType("object"); complex.name = rawType.getSimpleName(); @@ -282,6 +294,12 @@ private static Class componentType(final Type type) { return null; } + public boolean isCustom() { + return properties != null || + values != null || + pattern != null; + } + public boolean isObject() { return properties != null; } diff --git a/jooby-raml/src/test/java/org/jooby/raml/RamlTypeTest.java b/jooby-raml/src/test/java/org/jooby/raml/RamlTypeTest.java index 117795a1a8..0395cb31c5 100644 --- a/jooby-raml/src/test/java/org/jooby/raml/RamlTypeTest.java +++ b/jooby-raml/src/test/java/org/jooby/raml/RamlTypeTest.java @@ -6,6 +6,7 @@ import java.time.LocalDate; import java.util.Date; import java.util.Optional; +import java.util.UUID; import org.jooby.Upload; import org.jooby.internal.raml.RamlType; @@ -66,6 +67,13 @@ public void object() { " required: false", RamlType.parse(Person.class).toString()); } + @Test + public void uuid() { + assertEquals("uuid:\n" + + " type: string\n" + + " pattern: ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", RamlType.parse(UUID.class).toString()); + } + private Type optional(final Class type) { return Types.newParameterizedType(Optional.class, type); }