Skip to content

Commit c0fb24a

Browse files
committed
Register String[] mixin
Fixes spring-projectsgh-1666
1 parent d151568 commit c0fb24a

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public void setupModule(SetupContext context) {
8787
context.setMixInAnnotations(SignatureAlgorithm.class, JwsAlgorithmMixin.class);
8888
context.setMixInAnnotations(MacAlgorithm.class, JwsAlgorithmMixin.class);
8989
context.setMixInAnnotations(OAuth2TokenFormat.class, OAuth2TokenFormatMixin.class);
90+
context.setMixInAnnotations(String[].class, StringArrayMixin.class);
9091
}
9192

9293
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2024 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 com.fasterxml.jackson.annotation.JsonCreator;
19+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
20+
21+
/**
22+
* This mixin class is used to serialize/deserialize {@link String[]}.
23+
*
24+
* @author Nikola Jovanovic
25+
* @since 1.2.6
26+
* @see String[]
27+
*/
28+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
29+
abstract class StringArrayMixin {
30+
31+
@JsonCreator
32+
StringArrayMixin(String[] array) {
33+
}
34+
}

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/jackson2/OAuth2AuthorizationServerJackson2ModuleTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,8 @@ public class OAuth2AuthorizationServerJackson2ModuleTests {
4141
};
4242
private static final TypeReference<Set<String>> STRING_SET = new TypeReference<Set<String>>() {
4343
};
44+
private static final TypeReference<String[]> STRING_ARRAY = new TypeReference<String[]>() {
45+
};
4446

4547
private ObjectMapper objectMapper;
4648

@@ -71,4 +73,11 @@ public void readValueWhenLinkedHashSetThenSuccess() throws Exception {
7173
String json = this.objectMapper.writeValueAsString(set);
7274
assertThat(this.objectMapper.readValue(json, STRING_SET)).isEqualTo(set);
7375
}
76+
77+
@Test
78+
public void readValueWhenStringArrayThenSuccess() throws Exception {
79+
String[] array = new String[] {"one", "two"};
80+
String json = this.objectMapper.writeValueAsString(array);
81+
assertThat(this.objectMapper.readValue(json, STRING_ARRAY)).isEqualTo(array);
82+
}
7483
}

0 commit comments

Comments
 (0)