Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add UUID type to raml spec #770

Merged
merged 2 commits into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public String build(final List<RouteSpec> routes) {
Consumer<Type> 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);
}
};
Expand Down
18 changes: 18 additions & 0 deletions jooby-raml/src/main/java/org/jooby/internal/raml/RamlType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

private List<String> values;

private String pattern;

public RamlType(final String type) {
this.type = type;
}
Expand All @@ -65,6 +68,8 @@ public Map<String, RamlType> properties() {
return properties;
}

public String pattern(){ return pattern; }

@Override
public boolean equals(final Object obj) {
if (obj instanceof RamlType) {
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions jooby-raml/src/test/java/org/jooby/raml/RamlTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down