Skip to content

Commit

Permalink
Add support for custom user profile attributes (#749)
Browse files Browse the repository at this point in the history
* add support for custom user profile attributes

* added license header on new files

* fix build failure

* refactored

* refactored

* refactored
  • Loading branch information
arvindkrishnakumar-okta authored Aug 23, 2022
1 parent fedaab4 commit 361e42a
Show file tree
Hide file tree
Showing 7 changed files with 375 additions and 5 deletions.
4 changes: 1 addition & 3 deletions THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ limitations under the License.

This project includes:
A Jackson 2.x helper under The Apache Software License, Version 2.0
Animal Sniffer Annotations under MIT license
Apache Commons CLI under Apache License, Version 2.0
Apache Commons Codec under Apache License, Version 2.0
Apache Commons IO under Apache License, Version 2.0
Apache Commons Lang under Apache License, Version 2.0
Expand All @@ -37,7 +35,6 @@ This project includes:
error-prone annotations under Apache 2.0
FindBugs-jsr305 under The Apache Software License, Version 2.0
Generex under The Apache Software License, Version 2.0
Gson under Apache-2.0
Guava InternalFutureFailureAccess and InternalFutures under The Apache Software License, Version 2.0
Guava ListenableFuture only under The Apache Software License, Version 2.0
Guava: Google Core Libraries for Java under Apache License, Version 2.0
Expand Down Expand Up @@ -88,6 +85,7 @@ This project includes:
Spring Context under Apache License, Version 2.0
Spring Core under Apache License, Version 2.0
Spring Expression Language (SpEL) under Apache License, Version 2.0
Spring Retry under Apache 2.0
Spring Web under Apache License, Version 2.0
swagger-annotations under Apache License 2.0
swagger-compat-spec-parser under Apache License 2.0
Expand Down
18 changes: 18 additions & 0 deletions api/src/main/resources/custom_templates/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,29 @@
{{#vendorExtensions.x-class-extra-annotation}}
{{{vendorExtensions.x-class-extra-annotation}}}
{{/vendorExtensions.x-class-extra-annotation}}
{{#additionalPropertiesType}}
public class {{classname}} {{#vendorExtensions.x-implements}}{{#-first}}implements {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{#-last}} {{/-last}}{{/vendorExtensions.x-implements}}{
{{/additionalPropertiesType}}
{{^additionalPropertiesType}}
public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtensions.x-implements}}{{#-first}}implements {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{#-last}} {{/-last}}{{/vendorExtensions.x-implements}}{
{{/additionalPropertiesType}}

{{#serializableModel}}
private static final long serialVersionUID = 1L;

{{/serializableModel}}
{{#additionalPropertiesType}}
public Map<String, Object> additionalProperties = new java.util.LinkedHashMap<>();

public Map<String, Object> getAdditionalProperties() {
return additionalProperties;
}

public void setAdditionalProperties(Map<String, Object> additionalProperties) {
this.additionalProperties = additionalProperties;
}
{{/additionalPropertiesType}}

{{#vars}}
{{#isEnum}}
{{^isContainer}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.okta.commons.configcheck.ConfigurationValidator;
import com.okta.commons.http.config.Proxy;
Expand All @@ -37,6 +38,8 @@
import com.okta.sdk.impl.config.ResourcePropertiesSource;
import com.okta.sdk.impl.config.SystemPropertiesSource;
import com.okta.sdk.impl.config.YAMLPropertiesSource;
import com.okta.sdk.impl.serializer.UserProfileSerializer;
import com.okta.sdk.impl.deserializer.UserProfileDeserializer;
import com.okta.sdk.impl.io.ClasspathResource;
import com.okta.sdk.impl.io.DefaultResourceFactory;
import com.okta.sdk.impl.io.Resource;
Expand All @@ -54,6 +57,7 @@
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.openapitools.client.ApiClient;
import org.openapitools.client.model.UserProfile;
import org.openapitools.jackson.nullable.JsonNullableModule;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
Expand Down Expand Up @@ -299,6 +303,11 @@ private RestTemplate restTemplate(ClientConfiguration clientConfig) {
mapper.registerModule(new JavaTimeModule());
mapper.registerModule(new JsonNullableModule());

SimpleModule module = new SimpleModule();
module.addSerializer(UserProfile.class, new UserProfileSerializer());
module.addDeserializer(UserProfile.class, new UserProfileDeserializer());
mapper.registerModule(module);

List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(messageConverter);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* Copyright 2022-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 org.openapitools.client.model.UserProfile;

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

public class UserProfileDeserializer extends StdDeserializer<UserProfile> {

private static final long serialVersionUID = -6166716736969489408L;

private final ObjectMapper mapper = new ObjectMapper();

public UserProfileDeserializer() {
this(null);
}

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

@Override
public UserProfile 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>>(){});

UserProfile userProfile = new UserProfile();

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

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

switch (key) {
case UserProfile.JSON_PROPERTY_CITY:
userProfile.setCity(value);
break;

case UserProfile.JSON_PROPERTY_COST_CENTER:
userProfile.setCostCenter(value);
break;

case UserProfile.JSON_PROPERTY_COUNTRY_CODE:
userProfile.setCountryCode(value);
break;

case UserProfile.JSON_PROPERTY_DEPARTMENT:
userProfile.setDepartment(value);
break;

case UserProfile.JSON_PROPERTY_DISPLAY_NAME:
userProfile.setDisplayName(value);
break;

case UserProfile.JSON_PROPERTY_DIVISION:
userProfile.setDivision(value);
break;

case UserProfile.JSON_PROPERTY_EMAIL:
userProfile.setEmail(value);
break;

case UserProfile.JSON_PROPERTY_EMPLOYEE_NUMBER:
userProfile.setEmployeeNumber(value);
break;

case UserProfile.JSON_PROPERTY_FIRST_NAME:
userProfile.setFirstName(value);
break;

case UserProfile.JSON_PROPERTY_HONORIFIC_PREFIX:
userProfile.setHonorificPrefix(value);
break;

case UserProfile.JSON_PROPERTY_HONORIFIC_SUFFIX:
userProfile.setHonorificSuffix(value);
break;

case UserProfile.JSON_PROPERTY_LAST_NAME:
userProfile.setLastName(value);
break;

case UserProfile.JSON_PROPERTY_LOCALE:
userProfile.setLocale(value);
break;

case UserProfile.JSON_PROPERTY_LOGIN:
userProfile.setLogin(value);
break;

case UserProfile.JSON_PROPERTY_MANAGER:
userProfile.setManager(value);
break;

case UserProfile.JSON_PROPERTY_MANAGER_ID:
userProfile.setManagerId(value);
break;

case UserProfile.JSON_PROPERTY_MIDDLE_NAME:
userProfile.setMiddleName(value);
break;

case UserProfile.JSON_PROPERTY_MOBILE_PHONE:
userProfile.setMobilePhone(value);
break;

case UserProfile.JSON_PROPERTY_NICK_NAME:
userProfile.setNickName(value);
break;

case UserProfile.JSON_PROPERTY_ORGANIZATION:
userProfile.setOrganization(value);
break;

case UserProfile.JSON_PROPERTY_POSTAL_ADDRESS:
userProfile.setPostalAddress(value);
break;

case UserProfile.JSON_PROPERTY_PREFERRED_LANGUAGE:
userProfile.setPreferredLanguage(value);
break;

case UserProfile.JSON_PROPERTY_PRIMARY_PHONE:
userProfile.setPrimaryPhone(value);
break;

case UserProfile.JSON_PROPERTY_PROFILE_URL:
userProfile.setProfileUrl(value);
break;

case UserProfile.JSON_PROPERTY_SECOND_EMAIL:
userProfile.setSecondEmail(value);
break;

case UserProfile.JSON_PROPERTY_STATE:
userProfile.setState(value);
break;

case UserProfile.JSON_PROPERTY_STREET_ADDRESS:
userProfile.setStreetAddress(value);
break;

case UserProfile.JSON_PROPERTY_TIMEZONE:
userProfile.setTimezone(value);
break;

case UserProfile.JSON_PROPERTY_TITLE:
userProfile.setTitle(value);
break;

case UserProfile.JSON_PROPERTY_USER_TYPE:
userProfile.setUserType(value);
break;

case UserProfile.JSON_PROPERTY_ZIP_CODE:
userProfile.setZipCode(value);
break;

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

return userProfile;
}
}
Loading

0 comments on commit 361e42a

Please sign in to comment.