Skip to content

Commit 72eaacf

Browse files
committed
Cover required attributes
1 parent f7fe468 commit 72eaacf

File tree

2 files changed

+73
-27
lines changed

2 files changed

+73
-27
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/OAuth2AuthenticationExceptionMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
3636
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
3737
isGetterVisibility = JsonAutoDetect.Visibility.NONE)
38-
@JsonIgnoreProperties(ignoreUnknown = true, value = {"cause", "stackTrace"})
38+
@JsonIgnoreProperties(ignoreUnknown = true, value = {"cause", "stackTrace", "suppressedExceptions"})
3939
abstract class OAuth2AuthenticationExceptionMixin {
4040

4141
@JsonCreator
4242
OAuth2AuthenticationExceptionMixin(
4343
@JsonProperty("error") OAuth2Error error,
44-
@JsonProperty("message") String message) {
44+
@JsonProperty("detailMessage") String message) {
4545
}
4646
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/jackson2/OAuth2AuthenticationExceptionMixinTests.java

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
import org.springframework.security.jackson2.SecurityJackson2Modules;
2626
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
2727
import org.springframework.security.oauth2.core.OAuth2Error;
28+
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
2829

2930
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3032

3133
/**
3234
* Tests for {@link OAuth2AuthenticationExceptionMixin}.
@@ -45,44 +47,88 @@ public void setup() {
4547
this.mapper.registerModules(SecurityJackson2Modules.getModules(loader));
4648
}
4749

48-
// @formatter:off
49-
private static final String EXCEPTION_JSON
50-
= "\n{"
51-
+ "\n \"@class\": \"org.springframework.security.oauth2.core.OAuth2AuthenticationException\","
52-
+ "\n \"error\":"
53-
+ "\n {"
54-
+ "\n \"@class\":\"org.springframework.security.oauth2.core.OAuth2Error\","
55-
+ "\n \"errorCode\":\"authorization_request_not_found\","
56-
+ "\n \"description\":null,"
57-
+ "\n \"uri\":null"
58-
+ "\n },"
59-
+ "\n \"message\":\"[authorization_request_not_found] \","
60-
+ "\n \"suppressed\":[\"[Ljava.lang.Throwable;\",[]],"
61-
+ "\n \"localizedMessage\":\"[authorization_request_not_found] \""
62-
+ "\n}";
63-
// @formatter:on
50+
@Test
51+
public void serializeWhenMixinRegisteredThenSerializes() throws JsonProcessingException, JSONException {
52+
OAuth2AuthenticationException exception = new OAuth2AuthenticationException(new OAuth2Error(
53+
"[authorization_request_not_found] ",
54+
"Authorization Request Not Found",
55+
"/foo/bar"
56+
), "Authorization Request Not Found");
57+
58+
String serializedJson = mapper.writeValueAsString(exception);
59+
String expected = asJson(exception);
60+
JSONAssert.assertEquals(expected, serializedJson, true);
61+
}
6462

6563
@Test
66-
public void serializeOAuth2AuthenticationExceptionMixinTest() throws JsonProcessingException, JSONException {
67-
OAuth2Error oauth2Error = new OAuth2Error("authorization_request_not_found");
68-
OAuth2AuthenticationException exception = new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
64+
public void serializeWhenRequiredAttributesOnlyThenSerializes() throws JsonProcessingException, JSONException {
65+
OAuth2AuthenticationException exception = new OAuth2AuthenticationException(
66+
new OAuth2Error("[authorization_request_not_found] ")
67+
);
68+
6969
String serializedJson = mapper.writeValueAsString(exception);
70-
JSONAssert.assertEquals(EXCEPTION_JSON, serializedJson, true);
70+
String expected = asJson(exception);
71+
JSONAssert.assertEquals(expected, serializedJson, true);
72+
}
73+
74+
@Test
75+
public void deserializeWhenMixinRegisteredThenDeserializes() throws IOException {
76+
OAuth2AuthenticationException expected = new OAuth2AuthenticationException(new OAuth2Error(
77+
"[authorization_request_not_found] ",
78+
"Authorization Request Not Found",
79+
"/foo/bar"
80+
), "Authorization Request Not Found");
81+
82+
OAuth2AuthenticationException exception = mapper.readValue(asJson(expected), OAuth2AuthenticationException.class);
83+
assertThat(exception).isNotNull();
84+
assertThat(exception.getCause()).isNull();
85+
assertThat(exception.getMessage()).isEqualTo(expected.getMessage());
86+
87+
OAuth2Error oauth2Error = exception.getError();
88+
assertThat(oauth2Error).isNotNull();
89+
assertThat(oauth2Error.getErrorCode()).isEqualTo(expected.getError().getErrorCode());
90+
assertThat(oauth2Error.getDescription()).isEqualTo(expected.getError().getDescription());
91+
assertThat(oauth2Error.getUri()).isEqualTo(expected.getError().getUri());
7192
}
7293

7394
@Test
74-
public void deserializeOAuth2AuthenticationExceptionMixinTest() throws IOException {
75-
OAuth2AuthenticationException exception = mapper.readValue(EXCEPTION_JSON, OAuth2AuthenticationException.class);
95+
public void deserializeWhenRequiredAttributesOnlyThenDeserializes() throws IOException {
96+
OAuth2AuthenticationException expected = new OAuth2AuthenticationException(
97+
new OAuth2Error("[authorization_request_not_found] ")
98+
);
99+
100+
OAuth2AuthenticationException exception = mapper.readValue(asJson(expected), OAuth2AuthenticationException.class);
76101
assertThat(exception).isNotNull();
77102
assertThat(exception.getCause()).isNull();
78-
assertThat(exception.getMessage()).isEqualTo("[authorization_request_not_found] ");
79-
assertThat(exception.getLocalizedMessage()).isEqualTo("[authorization_request_not_found] ");
103+
assertThat(exception.getMessage()).isNull();
80104

81105
OAuth2Error oauth2Error = exception.getError();
82106
assertThat(oauth2Error).isNotNull();
83-
assertThat(oauth2Error.getErrorCode()).isEqualTo("authorization_request_not_found");
107+
assertThat(oauth2Error.getErrorCode()).isEqualTo(expected.getError().getErrorCode());
84108
assertThat(oauth2Error.getDescription()).isNull();
85109
assertThat(oauth2Error.getUri()).isNull();
86110
}
87111

112+
private String asJson(OAuth2AuthenticationException exception) {
113+
OAuth2Error error = exception.getError();
114+
// @formatter:off
115+
return "\n{"
116+
+ "\n \"@class\": \"org.springframework.security.oauth2.core.OAuth2AuthenticationException\","
117+
+ "\n \"error\":"
118+
+ "\n {"
119+
+ "\n \"@class\":\"org.springframework.security.oauth2.core.OAuth2Error\","
120+
+ "\n \"errorCode\":\"" + error.getErrorCode() + "\","
121+
+ "\n \"description\":" + jsonStringOrNull(error.getDescription()) + ","
122+
+ "\n \"uri\":" + jsonStringOrNull(error.getUri())
123+
+ "\n },"
124+
+ "\n \"detailMessage\":" + jsonStringOrNull(exception.getMessage())
125+
+ "\n}";
126+
// @formatter:on
127+
}
128+
129+
private String jsonStringOrNull(String input) {
130+
return input != null
131+
? "\"" + input + "\""
132+
: "null";
133+
}
88134
}

0 commit comments

Comments
 (0)