Skip to content

Commit a4f4a61

Browse files
authored
Merge pull request #770 from ajcamilo/raml-uuid
add UUID type to raml spec
2 parents 24df466 + feb85f5 commit a4f4a61

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

jooby-raml/src/main/java/org/jooby/internal/raml/RamlBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public String build(final List<RouteSpec> routes) {
272272
Consumer<Type> typeCollector = type -> {
273273
if (type != Object.class && type != void.class) {
274274
RamlType.parseAll(type).stream()
275-
.filter(t -> t.isObject() || t.isEnum())
275+
.filter(t -> t.isCustom())
276276
.forEach(types::add);
277277
}
278278
};

jooby-raml/src/main/java/org/jooby/internal/raml/RamlType.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Map;
3333
import java.util.Optional;
3434
import java.util.Set;
35+
import java.util.UUID;
3536

3637
import com.google.common.collect.ImmutableList;
3738

@@ -49,6 +50,8 @@ public class RamlType {
4950

5051
private List<String> values;
5152

53+
private String pattern;
54+
5255
public RamlType(final String type) {
5356
this.type = type;
5457
}
@@ -65,6 +68,8 @@ public Map<String, RamlType> properties() {
6568
return properties;
6669
}
6770

71+
public String pattern(){ return pattern; }
72+
6873
@Override
6974
public boolean equals(final Object obj) {
7075
if (obj instanceof RamlType) {
@@ -115,6 +120,9 @@ public String toString(int level) {
115120
if (values != null) {
116121
buff.append(indent(level)).append("enum: ").append(values.toString()).append("\n");
117122
}
123+
if (pattern != null) {
124+
buff.append(indent(level)).append("pattern: ").append(pattern).append("\n");
125+
}
118126
buff.setLength(buff.length() - 1);
119127
return buff.toString();
120128
}
@@ -196,6 +204,10 @@ private static RamlType simpleParse(final Type type) {
196204
enums.add(((Enum) value).name());
197205
}
198206
complex.values = enums;
207+
} else if (UUID.class.isAssignableFrom(rawType)) {
208+
complex = new RamlType("string");
209+
complex.name = "uuid";
210+
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}$";
199211
} else {
200212
complex = new RamlType("object");
201213
complex.name = rawType.getSimpleName();
@@ -282,6 +294,12 @@ private static Class<?> componentType(final Type type) {
282294
return null;
283295
}
284296

297+
public boolean isCustom() {
298+
return properties != null ||
299+
values != null ||
300+
pattern != null;
301+
}
302+
285303
public boolean isObject() {
286304
return properties != null;
287305
}

jooby-raml/src/test/java/org/jooby/raml/RamlTypeTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.time.LocalDate;
77
import java.util.Date;
88
import java.util.Optional;
9+
import java.util.UUID;
910

1011
import org.jooby.Upload;
1112
import org.jooby.internal.raml.RamlType;
@@ -66,6 +67,13 @@ public void object() {
6667
" required: false", RamlType.parse(Person.class).toString());
6768
}
6869

70+
@Test
71+
public void uuid() {
72+
assertEquals("uuid:\n" +
73+
" type: string\n" +
74+
" 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());
75+
}
76+
6977
private Type optional(final Class<?> type) {
7078
return Types.newParameterizedType(Optional.class, type);
7179
}

0 commit comments

Comments
 (0)