hilla
hilla copied to clipboard
@JsonProperty does not work as documented
The current documentation for the @JsonProperty
annotation is misleading: in reality it seem to have no effect on the TypeScript type generator (as of Vaadin 15.0.4).
- After reading the docs I would expect that
@JsonProperty("bookId") private String id;
in Java would lead tobookId: string;
in TypeScript. This is not the case; the generated TS type hasid: string
. - After reading the docs I would expect that
would lead topublic class Student { // NOTE: no field, only getter and setter @JsonProperty("name") public void setFirstName(String name) { this.name = name; } @JsonProperty("name") public String getFirstName() { return name; } }
name: string;
in TypeScript. This is not the case; the generated TS type has neithername
norfirstName
property at all.
The
@JsonProperty
annotationIt is used to define a method as a "setter" or "getter" for a logical property, or to define a field for being serialized and deserialized as a specific logical property.
The annotation value indicates the name of the property in the JSON object, by default it takes the java name of the method or field.
public class Student { @JsonProperty("bookId") private String id; private String name; @JsonProperty("name") public void setFirstName(String name) { this.name = name; } @JsonProperty("name") public String getFirstName() { return name; } @JsonProperty public int getRating() { return StudentRating.getRatingFor(name); } }
This seems to be a regression in 15.0.4. Now it should work again in 15.0.5, can you help to verify? @vlukashov
I can still see the following issues when testing with 17.0-SNAPSHOT / 3.2-SNAPSHOT:
-
@JsonProperty
on a field is ignored:
should producepublic static class Book { @JsonProperty("bookId") private String id; }
but producesinterface Book { bookId: string; }
which does not match the JSON object sent over the wire:interface Book { id: string; }
{ "bookId": null }
- Getters and setters are ignored:
should producepublic static class Student { @JsonIgnore private String id; public void setStudentId(String id) { this.id = id; } public String getStudentId() { return id; } }
but producesinterface Student { studentId: string; }
which does not match the JSON object sent over the wire:interface Student { }
{ "studentId": null }
-
@JsonProperty
on a getters / setters is ignored:
should producepublic static class Course { @JsonIgnore private String id; @JsonProperty("courseId") public void setId(String id) { this.id = id; } @JsonProperty("courseId") public String getId() { return id; } }
but producesinterface Course { courseId: string; }
which does not match the JSON object sent over the wire:interface Course { }
{ "courseId": null }
- The built-in nullability checker on Vaadin endpoints fails to detect the (unexpected) null property values returned by the backend in all the cases above. However, if
@JsonIgnore
is removed fromprivate String id
in the case 3, then the null checker actually does throw a server-side exception, as expected:2020-04-27 18:18:08.887 ERROR 32625 --- [nio-8080-exec-9] c.v.f.s.connect.VaadinConnectController : Unexpected return value in endpoint 'RequestEndpoint' method 'getCourse'. Unexpected null value in Java Bean type 'com.example.demo.backend.RequestEndpoint$Course' property 'id'. Got null value for type 'java.lang.String', which is neither Optional nor void
Any news about this? It's still broken in V20.
Should we at least update the documentation?
Please update documentation. Currently it is very misleading