Skip to content

Commit ded97e3

Browse files
committed
fixes for copilot review of 01/03
Signed-off-by: Laurent Arnal <laurent@clae.net>
1 parent de56843 commit ded97e3

4 files changed

Lines changed: 36 additions & 32 deletions

File tree

bundles/org.openhab.core.auth.oauth2client/src/main/java/org/openhab/core/auth/oauth2client/internal/OAuthConnector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public OAuthConnector(HttpClientFactory httpClientFactory, @Nullable Fields extr
9393
this.gson = getGson(gsonBuilder);
9494
}
9595

96-
public static Gson getGson(GsonBuilder gsonBuilder) {
96+
static Gson getGson(GsonBuilder gsonBuilder) {
9797
return gsonBuilder.setDateFormat(DateTimeType.DATE_PATTERN_JSON_COMPAT)
9898
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
9999
.registerTypeAdapter(OAuthResponseException.class,

bundles/org.openhab.core.auth.oauth2client/src/test/java/org/openhab/core/auth/oauth2client/internal/AccessTokenResponseExtraFieldTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
package org.openhab.core.auth.oauth2client.internal;
1414

15-
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.*;
1616

1717
import java.util.Map;
1818

@@ -24,7 +24,7 @@
2424
import com.google.gson.GsonBuilder;
2525

2626
/**
27-
* JUnit tests for {@link AccessTokenResponseExtraField}
27+
* JUnit tests for {@link AccessTokenResponse}
2828
*
2929
* @author Laurent Arnal - Initial contribution
3030
*/
@@ -39,14 +39,14 @@ public void testExtraFieldDeserialization() {
3939
String json = "{\"access_token\":\"AccessToken\",\"expires_in\":60,\"refresh_token\":\"RefreshToken\",\"app_client_id\":\"ApplicationClientId\"}";
4040
AccessTokenResponse atr = gson.fromJson(json, AccessTokenResponse.class);
4141

42-
assertEquals(atr.getAccessToken(), "AccessToken");
43-
assertEquals(atr.getExpiresIn(), 60);
44-
assertEquals(atr.getRefreshToken(), "RefreshToken");
42+
assertEquals("AccessToken", atr.getAccessToken());
43+
assertEquals(60, atr.getExpiresIn());
44+
assertEquals("RefreshToken", atr.getRefreshToken());
4545

4646
Map<String, String> extraFields = atr.getExtraFields();
4747

48-
assertEquals(extraFields.size(), 1);
49-
assertEquals(extraFields.containsKey("app_client_id"), true);
50-
assertEquals(extraFields.get("app_client_id"), "ApplicationClientId");
48+
assertEquals(1, extraFields.size());
49+
assertTrue(extraFields.containsKey("app_client_id"));
50+
assertEquals("ApplicationClientId", extraFields.get("app_client_id"));
5151
}
5252
}

bundles/org.openhab.core/src/main/java/org/openhab/core/auth/client/oauth2/AccessTokenResponse.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ public final class AccessTokenResponse implements Serializable, Cloneable {
133133
}
134134

135135
public void setExtraFields(Map<@NonNull String, @NonNull String> extraFields) {
136-
this.extraFields = extraFields != null ? extraFields : Collections.emptyMap();
136+
this.extraFields = (extraFields == null || extraFields.isEmpty()) ? Collections.emptyMap()
137+
: Map.copyOf(extraFields);
137138
}
138139

139140
/**
@@ -245,6 +246,6 @@ public boolean equals(Object thatAuthTokenObj) {
245246
public String toString() {
246247
return "AccessTokenResponse [accessToken=" + accessToken + ", tokenType=" + tokenType + ", expiresIn="
247248
+ expiresIn + ", refreshToken=" + refreshToken + ", scope=" + scope + ", state=" + state
248-
+ ", createdOn=" + createdOn + "]";
249+
+ ", createdOn=" + createdOn + ", extraFields= " + extraFields + "]";
249250
}
250251
}

bundles/org.openhab.core/src/main/java/org/openhab/core/auth/client/oauth2/AccessTokenResponseExtraFieldsAdapterFactory.java

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.google.gson.stream.JsonReader;
3131
import com.google.gson.stream.JsonWriter;
3232

33-
@NonNullByDefault
3433
/**
3534
* A {@link TypeAdapterFactory} that decorates the default {@link AccessTokenResponse} adapter in order to capture
3635
* additional fields returned by an OAuth 2.0 authorization server that are not part of the standard RFC 6749
@@ -39,10 +38,11 @@
3938
*
4039
* @author Laurent Arnal
4140
*/
41+
@NonNullByDefault
4242
public final class AccessTokenResponseExtraFieldsAdapterFactory implements TypeAdapterFactory {
4343

4444
private static final Set<String> KNOWN_FIELDS = Set.of("access_token", "token_type", "expires_in", "refresh_token",
45-
"scope", "state");
45+
"scope", "state", "created_on", "extra_fields");
4646

4747
@Override
4848
public <T> @Nullable TypeAdapter<T> create(@Nullable Gson gson, @Nullable TypeToken<T> type) {
@@ -67,29 +67,32 @@ public void write(JsonWriter out, T value) throws IOException {
6767
@Override
6868
public T read(JsonReader in) throws IOException {
6969
JsonElement tree = elementAdapter.read(in);
70-
if (tree != null) {
71-
JsonObject obj = tree.getAsJsonObject();
72-
73-
T parsed = delegate.fromJsonTree(tree);
74-
AccessTokenResponse response = (AccessTokenResponse) parsed;
75-
76-
Map<String, String> extras = new HashMap<>();
77-
for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
78-
String key = entry.getKey();
79-
if (KNOWN_FIELDS.contains(key)) {
80-
continue;
81-
}
82-
extras.put(key, toStringValue(gson, entry.getValue()));
83-
}
70+
if (tree == null) {
71+
return null;
72+
}
8473

85-
if (response != null) {
86-
response.setExtraFields(extras);
74+
if (!tree.isJsonObject()) {
75+
return delegate.fromJsonTree(tree);
76+
}
77+
78+
JsonObject obj = tree.getAsJsonObject();
79+
80+
T parsed = delegate.fromJsonTree(tree);
81+
AccessTokenResponse response = (AccessTokenResponse) parsed;
82+
83+
Map<String, String> extras = new HashMap<>();
84+
for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
85+
String key = entry.getKey();
86+
if (KNOWN_FIELDS.contains(key)) {
87+
continue;
8788
}
88-
return parsed;
89+
extras.put(key, toStringValue(gson, entry.getValue()));
8990
}
9091

91-
// Delegate adapter returned null; propagate null without extra fields
92-
return null;
92+
if (response != null) {
93+
response.setExtraFields(extras);
94+
}
95+
return parsed;
9396
}
9497
};
9598
}

0 commit comments

Comments
 (0)