Skip to content

Commit 71be32b

Browse files
Steve Riesenbergsjohnr
authored andcommitted
Add support for deserializing LinkedHashSet
This is needed because OAuth2ClientCredentialsAuthenticationProvider stores authorized scopes in a LinkedHashSet. Closes gh-457
1 parent 4b19637 commit 71be32b

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/jackson2/OAuth2AuthorizationServerJackson2Module.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.time.Duration;
1919
import java.util.Collections;
2020
import java.util.HashSet;
21+
import java.util.LinkedHashSet;
2122

2223
import com.fasterxml.jackson.core.Version;
2324
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -71,6 +72,7 @@ public void setupModule(SetupContext context) {
7172
context.setMixInAnnotations(Collections.unmodifiableMap(Collections.emptyMap()).getClass(),
7273
UnmodifiableMapMixin.class);
7374
context.setMixInAnnotations(HashSet.class, HashSetMixin.class);
75+
context.setMixInAnnotations(LinkedHashSet.class, HashSetMixin.class);
7476
context.setMixInAnnotations(OAuth2AuthorizationRequest.class, OAuth2AuthorizationRequestMixin.class);
7577
context.setMixInAnnotations(Duration.class, DurationMixin.class);
7678
context.setMixInAnnotations(SignatureAlgorithm.class, SignatureAlgorithmMixin.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2020-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.server.authorization.jackson2;
17+
18+
import java.util.Arrays;
19+
import java.util.Collections;
20+
import java.util.HashMap;
21+
import java.util.HashSet;
22+
import java.util.LinkedHashSet;
23+
import java.util.Map;
24+
import java.util.Set;
25+
26+
import com.fasterxml.jackson.core.type.TypeReference;
27+
import com.fasterxml.jackson.databind.ObjectMapper;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Tests for {@link OAuth2AuthorizationServerJackson2Module}.
35+
*
36+
* @author Steve Riesenberg
37+
*/
38+
public class OAuth2AuthorizationServerJackson2ModuleTests {
39+
40+
private static final TypeReference<Map<String, Object>> STRING_OBJECT_MAP = new TypeReference<Map<String, Object>>() {
41+
};
42+
private static final TypeReference<Set<String>> STRING_SET = new TypeReference<Set<String>>() {
43+
};
44+
45+
private ObjectMapper objectMapper;
46+
47+
@Before
48+
public void setup() {
49+
this.objectMapper = new ObjectMapper();
50+
this.objectMapper.registerModule(new OAuth2AuthorizationServerJackson2Module());
51+
}
52+
53+
@Test
54+
public void readValueWhenUnmodifiableMapThenSuccess() throws Exception {
55+
Map<String, Object> map = Collections.unmodifiableMap(new HashMap<>(Collections.singletonMap("key", "value")));
56+
String json = this.objectMapper.writeValueAsString(map);
57+
assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(map);
58+
}
59+
60+
@Test
61+
public void readValueWhenHashSetThenSuccess() throws Exception {
62+
Set<String> set = new HashSet<>(Arrays.asList("one", "two"));
63+
String json = this.objectMapper.writeValueAsString(set);
64+
assertThat(this.objectMapper.readValue(json, STRING_SET)).isEqualTo(set);
65+
}
66+
67+
// gh-457
68+
@Test
69+
public void readValueWhenLinkedHashSetThenSuccess() throws Exception {
70+
Set<String> set = new LinkedHashSet<>(Arrays.asList("one", "two"));
71+
String json = this.objectMapper.writeValueAsString(set);
72+
assertThat(this.objectMapper.readValue(json, STRING_SET)).isEqualTo(set);
73+
}
74+
}

0 commit comments

Comments
 (0)