Skip to content

Commit

Permalink
Fix GroupsAPI missing Custom group profile attributes (#987)
Browse files Browse the repository at this point in the history
* Fix GroupsAPI missing Custom group profile attributes

* cleanup unused imports and update license header
  • Loading branch information
arvindkrishnakumar-okta authored Mar 6, 2024
1 parent fa8537e commit bfd8297
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.okta.sdk.client.ClientBuilder;
import com.okta.sdk.impl.api.DefaultClientCredentialsResolver;
import com.okta.sdk.impl.config.*;
import com.okta.sdk.impl.deserializer.GroupProfileDeserializer;
import com.okta.sdk.impl.deserializer.UserProfileDeserializer;
import com.okta.sdk.impl.io.ClasspathResource;
import com.okta.sdk.impl.io.DefaultResourceFactory;
Expand All @@ -42,11 +43,13 @@
import com.okta.sdk.impl.oauth2.AccessTokenRetrieverService;
import com.okta.sdk.impl.oauth2.AccessTokenRetrieverServiceImpl;
import com.okta.sdk.impl.oauth2.OAuth2ClientCredentials;
import com.okta.sdk.impl.serializer.GroupProfileSerializer;
import com.okta.sdk.impl.serializer.UserProfileSerializer;
import com.okta.sdk.impl.util.ConfigUtil;
import com.okta.sdk.impl.util.DefaultBaseUrlResolver;

import com.okta.sdk.impl.retry.OktaHttpRequestRetryStrategy;
import com.okta.sdk.resource.model.GroupProfile;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.config.ConnectionConfig;
Expand Down Expand Up @@ -460,6 +463,8 @@ private void addCustomSerializerAndDeserializers(ApiClient apiClient) {
SimpleModule module = new SimpleModule();
module.addSerializer(UserProfile.class, new UserProfileSerializer());
module.addDeserializer(UserProfile.class, new UserProfileDeserializer());
module.addSerializer(GroupProfile.class, new GroupProfileSerializer());
module.addDeserializer(GroupProfile.class, new GroupProfileDeserializer());
mapper.registerModule(module);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2024-Present Okta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.okta.sdk.impl.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.okta.sdk.resource.model.GroupProfile;

import java.io.IOException;
import java.util.Map;

public class GroupProfileDeserializer extends StdDeserializer<GroupProfile> {

private static final long serialVersionUID = -1809919319486559586L;

private final ObjectMapper mapper = new ObjectMapper();

public GroupProfileDeserializer() {
this(null);
}

public GroupProfileDeserializer(Class<?> vc) {
super(vc);
}

@Override
public GroupProfile deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {

JsonNode node = jp.getCodec().readTree(jp);

Map<String, Object> profileMap = mapper.convertValue(node, new TypeReference<Map<String, Object>>(){});

GroupProfile groupProfile = new GroupProfile();

for (Map.Entry<String, Object> entry : profileMap.entrySet()) {

String key = entry.getKey();
Object value = entry.getValue();

switch (key) {
case GroupProfile.JSON_PROPERTY_NAME:
groupProfile.setName((String) value);
break;

case GroupProfile.JSON_PROPERTY_DESCRIPTION:
groupProfile.setDescription((String) value);
break;

default:
groupProfile.getAdditionalProperties().put(key, value);
}
}

return groupProfile;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2024-Present Okta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.okta.sdk.impl.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.okta.commons.lang.Strings;
import com.okta.sdk.resource.model.GroupProfile;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

public class GroupProfileSerializer extends StdSerializer<GroupProfile> {

private static final long serialVersionUID = -7330836671206521399L;

public GroupProfileSerializer() {
this(null);
}

public GroupProfileSerializer(Class<GroupProfile> t) {
super(t);
}

@Override
public void serialize(GroupProfile groupProfile, JsonGenerator jgen, SerializerProvider provider) throws IOException {

jgen.writeStartObject();

if (Strings.hasText(groupProfile.getName())) {
jgen.writeStringField(GroupProfile.JSON_PROPERTY_NAME, groupProfile.getName());
}

if (Strings.hasText(groupProfile.getDescription())) {
jgen.writeStringField(GroupProfile.JSON_PROPERTY_DESCRIPTION, groupProfile.getDescription());
}

Map<String, Object> additionalProperties = groupProfile.getAdditionalProperties();

if (Objects.nonNull(additionalProperties) && !additionalProperties.isEmpty()) {
for (Map.Entry<String, Object> entry : additionalProperties.entrySet()) {
jgen.writeObjectField(entry.getKey(), entry.getValue());
}
}

jgen.writeEndObject();
}
}

0 comments on commit bfd8297

Please sign in to comment.