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

Specifying a View when using jackson2 #649

Closed
mikoet opened this issue Mar 10, 2021 · 7 comments
Closed

Specifying a View when using jackson2 #649

mikoet opened this issue Mar 10, 2021 · 7 comments

Comments

@mikoet
Copy link

mikoet commented Mar 10, 2021

Hey,

I just came across this generator, and it seems to be mostly what I was needing.

But in my Java project I'm making use of com.fasterxml.jackson.annotation.View for Jackson2, that means I can generate different JSON data from the same object – depending on the context I am in.

I have looked into the docs but so far did not find if I could possibly specify a View that should be used. Any idea?

Example

My views:

public class Views {
	public static class BaseConfig {
	}

	public static class REST {
	}

	public static class Exclude {
	}
}

Sample of a persistent class that is also used in JSON communication:

package foo.bar;

import com.fasterxml.jackson.annotation.JsonView;

@Data
@Entity
public class SomeClass
{
	@Id
	@JsonView({Views.BaseConfig.class, Views.REST.class})
	private Integer id;

	@JsonView({Views.BaseConfig.class, Views.REST.class})
	private Integer parentID;
	@JoinColumn(name = "parentID", insertable = false, updatable = false)
	@ManyToOne(targetEntity = SomeClass.class, fetch = FetchType.LAZY)
	@JsonView(Views.Exclude.class) // @JsonIgnore would be an alternative for this
	private SomeClass parent;

	@Column(length = 32, nullable = false)
	@JsonView({Views.REST.class})
	private String someProperty;

	@Column(length = 32, nullable = false)
	@JsonView({Views.BaseConfig.class})
	private Long anotherProperty;
}

I then configure an ObjectMapper as follows for the JSON mapping on my REST API.

ObjectMapper mapper = new ObjectMapper();
mapper.setConfig(mapper.getSerializationConfig().withView(Views.REST.class));
@vojtechhabarta
Copy link
Owner

Hey, this is currently not possible. If you need just one view it should be relatively easy to add this feature. I will try it.
I am wondering if somebody would want to generate more variants (views) in the same run but it seem not much probable. (It would be much more complex.)

@mikoet
Copy link
Author

mikoet commented Mar 15, 2021

@vojtechhabarta I see, thanks for your investigation! I do indeed serve both views, REST and BaseConfig, to my Angular application (TypeScript). The former (REST) is used for the administration of those entities (as an admin) and the later (BaseConfig) is for app configuration in the frontend for normal users.
But I'd be totally fine if I could just specify one view and make two runs. That would be still so much better than writing the interfaces on my own.

@vojtechhabarta
Copy link
Owner

I added jackson2Configuration.view configuration parameter which activates one of the views used in @JsonView annotations.

Maven sample:

<configuration>
    <jsonLibrary>jackson2</jsonLibrary>
    <jackson2Configuration>
        <view>foo.bar.Views$REST</view>
    </jackson2Configuration>
</configuration>

@mikoet
Copy link
Author

mikoet commented May 18, 2021

Sounds great, I will probably check it out on the weekend. Thanks a lot already!

@vojtechhabarta
Copy link
Owner

Released in v2.32.889.

@kimmymonassar
Copy link

Hey!

Im trying to switch view to fix some serializer errors.

This is my code:

<configuration>
  <jackson2Configuration>
     <view>$fieldVisibility</view>
  </jackson2Configuration>
</configuration>

Shouldnt this mean that i now have an elevated fieldvisibility? I cant see anything about it in the output and I can't solve my serializer problems.

Worth to note is that im only using pom.xml also since that has been enough until now

@mikoet
Copy link
Author

mikoet commented Jun 13, 2021

Please excuse my belated reply @vojtechhabarta, I just got to try it today. There is nothing I could complain about, it works like a charm so far 😊. So finally I don't have to updated my typescript REST models on my own anymore. Thanks again for implementing this, I guess I owe you a pizza and a beer :-)

mohasarc pushed a commit to mohasarc/typescript-generator that referenced this issue Jun 24, 2021
mohasarc pushed a commit to mohasarc/typescript-generator that referenced this issue Jun 24, 2021
mohasarc pushed a commit to mohasarc/typescript-generator that referenced this issue Jun 24, 2021
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

3 participants