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

Wrong property parsing #617

Closed
SButterfly opened this issue Jan 11, 2021 · 4 comments
Closed

Wrong property parsing #617

SButterfly opened this issue Jan 11, 2021 · 4 comments

Comments

@SButterfly
Copy link
Contributor

After some release my method

public DisplaySeason setupSeason(Consumer<Season> seasonConsumer) {
        seasonConsumer.accept(season);
        return this;
}

was treated as property:

export interface DisplaySeason {
  ....
  upSeason: Consumer<Season>;
}

I temporary fix it by marking @JsonIgnore. Is there any better solution?

@vojtechhabarta
Copy link
Owner

vojtechhabarta commented Jan 12, 2021

Originally typescript-generator only used Jackson's BeanSerializer to get list of properties. But this list only contains properties that can be serialized, there can also be properties that can be deserialized. So starting from version 2.27 typescript-generator uses also BeanDeserializer to get list of properties. This was added mainly for #579.

The problem here is really curious. Obviously Jackson considers setupSeason method to be a setter for upSeason property which looks strange to me but we can see it using following sample code:

public static class DisplaySeason {
    private String value;
    public void setupSeason(String season) {
        value = season;
    }
}
public static void main(String[] args) throws JsonProcessingException {
    final ObjectMapper objectMapper = new ObjectMapper();
    final String json = "{\"upSeason\":\"abc\"}";
    final DisplaySeason displaySeason = objectMapper.readValue(json, DisplaySeason.class);
    System.out.println(displaySeason.value);
}

This code parses JSON, reads upSeason property, sets it using setupSeason method and prints abc value.

Apart from annotating setupSeason with @JsonIgnore you can also annotate DisplaySeason class with @JsonAutoDetect(setterVisibility = JsonAutoDetect.Visibility.NONE).

@vojtechhabarta
Copy link
Owner

I realised that at least Jackson is consistent with Java Beans API. If I try following code:

BeanInfo beanInfo = Introspector.getBeanInfo(DisplaySeason.class);
Stream.of(beanInfo.getPropertyDescriptors()).forEach(System.out::println);

it also prints property upSeason.

@vojtechhabarta
Copy link
Owner

Actually there is one difference between Jackson and Java Beans. When method return type is void both consider this method a setter but when the return type is DisplaySeason only Jackson consider it a setter.

@vojtechhabarta
Copy link
Owner

Another possibility how to exclude setter methods is to configure ObjectMapper used by typescript-generator using jackson2Configuration parameter. Here is Gradle sample:

generateTypeScript {
    jsonLibrary = 'jackson2'
    jackson2Configuration = [
        setterVisibility: 'NONE'
    ]
}

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

No branches or pull requests

2 participants