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

Read-only and write-only properties (Jackson2, JSDoc tags) #579

Merged
merged 1 commit into from
Nov 17, 2020

Conversation

vojtechhabarta
Copy link
Owner

When communicating using JSON objects over HTTP usually most of the properties are sent in both directions (GET vs. POST, PUT). But sometimes some properties are sent only from server to client and others are sent only from client to server. Classical example can be following REST object:

public class User {

    public String name;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    public String id;

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    public String password;
}

In this example when client application is creating new User it sends name and password properties then server generates id property and returns User object containing id and name properties. As you can see Jackson2 library allows us to mark properties read-only or write-only using @JsonProperty annotation.

This PR provides possibility to leverage Jackson2 support for read-only and write-only properties and generate for example following TypeScript declaration:

interface User {
    name: string;
    /**
     * @readonly
     */
    id: string;
    /**
     * @writeonly
     */
    password: string;
}

This feature is turned off by default and can be turned on using generateReadonlyAndWriteonlyJSDocTags parameter.

Previously list of properties was obtained from Jackson2 serializer but now it is obtained also from deserializer and those lists are combined. So now typescript-generator can produce more properties than before which can be unexpected. This behavior is the same regardless the feature is turned on or off.

Read-only and write-only properties can be used not only for real read-only or write-only properties like in id and password example but we can also have resource object where some property has different "shape" when it is read or written by client. For example we can have logical property users (access control list) which is sent in "expanded" form by server but client sends just user IDs:

interface Resource {
    name: string;
    /**
     * @readonly
     */
    user: User[];
    /**
     * @writeonly
     */
    userIDs: string[];
}

Currently this feature is limited to Jackson2 library (input) and JSDoc tags (output) but it can be expanded in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant