Skip to content

Commit a8e21df

Browse files
Merge pull request #93 from vojtechhabarta/java8optional
Support for Java 8 Optional
2 parents 68733cf + 8cb4639 commit a8e21df

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed

typescript-generator-core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
<artifactId>jackson-jaxrs-json-provider</artifactId>
2525
<version>2.6.3</version>
2626
</dependency>
27+
<!-- <dependency>
28+
<groupId>com.fasterxml.jackson.datatype</groupId>
29+
<artifactId>jackson-datatype-jdk8</artifactId>
30+
<version>2.6.3</version>
31+
</dependency>-->
2732
<dependency>
2833
<groupId>javax.ws.rs</groupId>
2934
<artifactId>javax.ws.rs-api</artifactId>

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/DefaultTypeProcessor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public Result processType(Type javaType, Context context) {
4141
final Result result = context.processType(parameterizedType.getActualTypeArguments()[1]);
4242
return new Result(new TsType.IndexedArrayType(TsType.String, result.getTsType()), result.getDiscoveredClasses());
4343
}
44+
if (javaClass.getName().equals("java.util.Optional")) {
45+
return context.processType(parameterizedType.getActualTypeArguments()[0]);
46+
}
4447
// consider it structural
4548
return new Result(new TsType.ReferenceType(context.getSymbol(javaClass)), javaClass);
4649
}

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/GenericsTypeProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class GenericsTypeProcessor implements TypeProcessor {
1111
@Override
1212
public TypeProcessor.Result processType(Type javaType, TypeProcessor.Context context) {
1313
if (javaType instanceof TypeVariable) {
14-
final TypeVariable<?> typeVariable = (TypeVariable) javaType;
14+
final TypeVariable<?> typeVariable = (TypeVariable<?>) javaType;
1515
return new Result(new TsType.GenericVariableType(typeVariable.getName()));
1616
}
1717
if (javaType instanceof Class) {
@@ -31,7 +31,7 @@ public TypeProcessor.Result processType(Type javaType, TypeProcessor.Context con
3131
}
3232

3333
private Result processGenericClass(Class<?> rawType, Type[] typeArguments, TypeProcessor.Context context) {
34-
if (!Collection.class.isAssignableFrom(rawType) && !Map.class.isAssignableFrom(rawType)) {
34+
if (!Collection.class.isAssignableFrom(rawType) && !Map.class.isAssignableFrom(rawType) && !rawType.getName().equals("java.util.Optional")) {
3535
final List<Class<?>> discoveredClasses = new ArrayList<>();
3636
// raw type
3737
final Symbol rawSymbol = context.getSymbol(rawType);
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
2+
package cz.habarta.typescript.generator;
3+
4+
//import com.fasterxml.jackson.annotation.JsonInclude;
5+
//import com.fasterxml.jackson.databind.ObjectMapper;
6+
//import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
7+
//import java.util.Objects;
8+
//import java.util.Optional;
9+
//import org.junit.Assert;
10+
//import org.junit.Test;
11+
12+
13+
// Java 8 is required for this test
14+
// TODO uncomment on Java 8
15+
16+
public class OptionalTest {
17+
18+
// @Test
19+
// public void test() {
20+
// final String output = new TypeScriptGenerator(TestUtils.settings()).generateTypeScript(Input.from(Person.class));
21+
// Assert.assertEquals(
22+
// "interface Person {\n" +
23+
// " name: string;\n" +
24+
// " email: string;\n" +
25+
// "}",
26+
// output.trim());
27+
// }
28+
//
29+
// @Test
30+
// public void testJackson2OptionalSupport() throws Exception {
31+
// final ObjectMapper objectMapper = new ObjectMapper()
32+
// .registerModule(new Jdk8Module())
33+
// .setSerializationInclusion(JsonInclude.Include.NON_NULL);
34+
//
35+
// final Person personWithEmail = new Person("afh", Optional.of("[email protected]"));
36+
// final Person personWithEmptyEmail = new Person("afh", Optional.<String>empty());
37+
// final Person personWithoutEmail = new Person("afh", null);
38+
//
39+
// final String jsonWithEmail = "{'name':'afh','email':'[email protected]'}".replace('\'', '\"');
40+
// final String jsonWithNullEmail = "{'name':'afh','email':null}".replace('\'', '\"');
41+
// final String jsonWithoutEmail = "{'name':'afh'}".replace('\'', '\"');
42+
//
43+
// Assert.assertEquals(jsonWithEmail, objectMapper.writeValueAsString(personWithEmail));
44+
// Assert.assertEquals(jsonWithNullEmail, objectMapper.writeValueAsString(personWithEmptyEmail));
45+
// Assert.assertEquals(jsonWithoutEmail, objectMapper.writeValueAsString(personWithoutEmail));
46+
//
47+
// Assert.assertEquals(personWithEmail, objectMapper.readValue(jsonWithEmail, Person.class));
48+
// Assert.assertEquals(personWithEmptyEmail, objectMapper.readValue(jsonWithNullEmail, Person.class));
49+
// Assert.assertEquals(personWithoutEmail, objectMapper.readValue(jsonWithoutEmail, Person.class));
50+
// }
51+
//
52+
// private static class Person {
53+
// public String name;
54+
// public Optional<String> email;
55+
//
56+
// public Person() {
57+
// }
58+
//
59+
// public Person(String name, Optional<String> email) {
60+
// this.name = name;
61+
// this.email = email;
62+
// }
63+
//
64+
// @Override
65+
// public int hashCode() {
66+
// int hash = 7;
67+
// hash = 53 * hash + Objects.hashCode(this.name);
68+
// hash = 53 * hash + Objects.hashCode(this.email);
69+
// return hash;
70+
// }
71+
//
72+
// @Override
73+
// public boolean equals(Object obj) {
74+
// if (this == obj) {
75+
// return true;
76+
// }
77+
// if (obj == null) {
78+
// return false;
79+
// }
80+
// if (getClass() != obj.getClass()) {
81+
// return false;
82+
// }
83+
// final Person other = (Person) obj;
84+
// if (!Objects.equals(this.name, other.name)) {
85+
// return false;
86+
// }
87+
// if (!Objects.equals(this.email, other.email)) {
88+
// return false;
89+
// }
90+
// return true;
91+
// }
92+
//
93+
// @Override
94+
// public String toString() {
95+
// return "Person{" + "name=" + name + ", email=" + email + '}';
96+
// }
97+
// }
98+
99+
}

0 commit comments

Comments
 (0)