Skip to content

Commit c9b76b0

Browse files
Support @JsonUnwrapped used on backing fields (refs #247)
1 parent 5380856 commit c9b76b0

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/Jackson2Parser.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,11 @@ public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
113113
});
114114
// @JsonUnwrapped
115115
PropertyModel.PullProperties pullProperties = null;
116-
final Member originalMember = beanPropertyWriter.getMember().getMember();
117-
if (originalMember instanceof AccessibleObject) {
118-
final AccessibleObject accessibleObject = (AccessibleObject) originalMember;
119-
final JsonUnwrapped annotation = accessibleObject.getAnnotation(JsonUnwrapped.class);
120-
if (annotation != null && annotation.enabled()) {
121-
pullProperties = new PropertyModel.PullProperties(annotation.prefix(), annotation.suffix());
122-
}
116+
final JsonUnwrapped annotation = beanPropertyWriter.getAnnotation(JsonUnwrapped.class);
117+
if (annotation != null && annotation.enabled()) {
118+
pullProperties = new PropertyModel.PullProperties(annotation.prefix(), annotation.suffix());
123119
}
124-
properties.add(processTypeAndCreateProperty(beanPropertyWriter.getName(), propertyType, optional, sourceClass.type, originalMember, pullProperties));
120+
properties.add(processTypeAndCreateProperty(beanPropertyWriter.getName(), propertyType, optional, sourceClass.type, propertyMember, pullProperties));
125121
}
126122
}
127123
if (sourceClass.type.isEnum()) {

typescript-generator-core/src/test/java/cz/habarta/typescript/generator/JsonUnwrappedTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ public void test() {
4848
Assert.assertEquals(expected.trim(), output.trim());
4949
}
5050

51+
@Test
52+
public void testPrivateField() {
53+
final Settings settings = TestUtils.settings();
54+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(Person2.class));
55+
final String expected = "\n"
56+
+ "interface Person2 {\n"
57+
+ " first: string;\n"
58+
+ " last: string;\n"
59+
+ "}\n"
60+
+ "\n"
61+
+ "interface Name {\n"
62+
+ " first: string;\n"
63+
+ " last: string;\n"
64+
+ "}\n"
65+
+ "";
66+
Assert.assertEquals(expected.trim(), output.trim());
67+
}
68+
5169
public static class Person {
5270
@JsonUnwrapped(prefix = "A", suffix = "A")
5371
public Parent parentA;
@@ -69,6 +87,15 @@ public static class Name {
6987
public String first, last;
7088
}
7189

90+
public static class Person2 {
91+
@JsonUnwrapped
92+
private Name name;
93+
94+
public Name getName() {
95+
return name;
96+
}
97+
}
98+
7299
public static void main(String[] args) throws Exception {
73100
final Parent parent = new Parent();
74101
parent.age = 18;
@@ -84,10 +111,13 @@ public static void main(String[] args) throws Exception {
84111
final Person person = new Person();
85112
person.parentA = parent;
86113
person.parentB = parent;
114+
final Person2 person2 = new Person2();
115+
person2.name = parent.name;
87116
final ObjectMapper objectMapper = new ObjectMapper();
88117
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
89118
objectMapper.setDefaultPrettyPrinter(new StandardJsonPrettyPrinter());
90119
System.out.println(objectMapper.writeValueAsString(person));
120+
System.out.println(objectMapper.writeValueAsString(person2));
91121
}
92122

93123
}

0 commit comments

Comments
 (0)