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

Change Immutability for OAuth2AuthorizedClient #8905

Closed
dgempiuc opened this issue Aug 4, 2020 · 1 comment
Closed

Change Immutability for OAuth2AuthorizedClient #8905

dgempiuc opened this issue Aug 4, 2020 · 1 comment
Assignees
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) status: duplicate A duplicate of another issue

Comments

@dgempiuc
Copy link

dgempiuc commented Aug 4, 2020

Expected Behavior

OAuth2AuthorizedClient and its nested classes are designed immutable. Can you change this behaviour?

Current Behavior

There are currently only "final" fields, full-args constructor and getter methods.

Context

I want to store this class in Redis and it's fine by using Jackson. But, there is exception during Deserialization, because it first creates object with no-arg constructor, then uses setter methods. Because of that, I can't retrieve record from Redis.

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of org.springframework.security.oauth2.client.OAuth2AuthorizedClient (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

There is an option provided by Jackson to solve this. mix-in classes. (https://github.com/FasterXML/jackson-docs/wiki/JacksonMixInAnnotations)

I have created a mix-in class for OAuth2AuthorizedClient, but this time, its inner classes need their own mixin classes. so there will be some many dummy mixin classes and configurations, so it will causes complexity.

public class RedisObjectMapper implements RedisSerializer<OAuth2AuthorizedClient> {

    private final ObjectMapper om;

    public RedisObjectMapper() {
        this.om = new ObjectMapper()
               .addMixIn(OAuth2AuthorizedClient.class, TargetDataMixin.class);
    }

    @Override
    public byte[] serialize(OAuth2AuthorizedClient o) throws SerializationException {
        try {
            return om.writeValueAsBytes(o);
        } catch (JsonProcessingException e) {
            ...
        }
    }

    @Override
    public OAuth2AuthorizedClient deserialize(byte[] bytes) throws SerializationException {
        if(bytes == null){
            return null;
        }
        try {
            return  om.readValue(bytes, OAuth2AuthorizedClient.class);
        } catch (Exception e) {
            ...
        }
    }

}
public abstract class TargetDataMixin {
    @JsonCreator
    public TargetDataMixin(@JsonProperty("clientRegistration") ClientRegistration clientRegistration,
                           @JsonProperty("principalName") String principalName,
                           @JsonProperty("accessToken") OAuth2AccessToken accessToken,
                           @JsonProperty("refreshToken") OAuth2RefreshToken refreshToken) {
    }
}
@dgempiuc dgempiuc added status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement labels Aug 4, 2020
@jgrandja
Copy link
Contributor

jgrandja commented Aug 6, 2020

@dgempiuc Jackson support has been added in the 5.3.0 release via gh-7873. This includes OAuth2AuthorizedClientMixin and it's associated classes. See gh-4886 and gh-7889 for additional details.

I'll close this issue as a duplicate.

@jgrandja jgrandja closed this as completed Aug 6, 2020
@jgrandja jgrandja self-assigned this Aug 6, 2020
@jgrandja jgrandja added in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement labels Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

2 participants