havardh / javaflow

Java model to flowtype converter
22 stars 5 forks source link

Support jackson annotations: @JsonIgnore, @JsonProperty, @JsonGetter #78

Open spirit93 opened 5 years ago

spirit93 commented 5 years ago

For example:

package domain.document;

import domain.person.Person;

public class Document {
    private long id;
    private Person person;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
}
package domain.person;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import domain.document.Document;

import java.util.Set;

public class Person {
    private long id;
    @JsonProperty("documents")
    private Set<Document> documentSet;
    @JsonIgnore
    private String password;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Set<Document> getDocumentSet() {
        return documentSet;
    }

    public void setDocumentSet(Set<Document> documentSet) {
        this.documentSet = documentSet;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Result: javaflow domain/

/* eslint-disable no-use-before-define */
/* Generated by javaflow 1.5.0 */
export type Document = {
  id: number,
  person: Person,
};

export type Person = {
  id: number,
  documentSet: Array<Document>,
  password: string,
};

But want:

  id: number,
  person: Person,
};

export type Person = {
  id: number,
  documents: Array<Document>,
};
havardh commented 5 years ago

@spirit93: This looks like a nice suggestion :) As Jackson is a very standard way for serialising to json I see no issues with adding this to the core of Javaflow.

I would propose this requires similar interactions between the ClassVisitor, Field and FieldDefinitionWriter classes as the implementation of the Nullable annotation. And the JsonGetter being the somewhat harder to add.

Feel free to create PRs for these if you have the time, It can take a while before I have, but I see to that changes are merged and released :)