Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package com.microsoft.graph.serializer;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.CaseFormat;
import com.google.gson.JsonObject;
import com.microsoft.graph.logger.ILogger;

import java.util.Locale;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/** This class provides methods to get the derived class corresponding to the OData type when deserializing payloads. */
/**
* This class provides methods to get the derived class corresponding to the OData type when deserializing payloads.
*/
public class DerivedClassIdentifier {

private final static String ODATA_TYPE_KEY = "@odata.type";

private final ILogger logger;

/**
* Creates a new instance of the dereived class identifier.
*
* @param logger The logger to use.
*/
public DerivedClassIdentifier(@Nonnull ILogger logger) {
this.logger = Objects.requireNonNull(logger, "logger parameter cannot be null");;
this.logger = Objects.requireNonNull(logger, "logger parameter cannot be null");
;
}

/**
Expand All @@ -29,7 +36,7 @@ public DerivedClassIdentifier(@Nonnull ILogger logger) {
*
* @param jsonObject the raw JSON object of the response
* @param parentClass the parent class the derived class should inherit from
* @return the derived class if found, or null if not applicable
* @return the derived class if found, or null if not applicable
*/
@Nullable
public Class<?> identify(@Nonnull final JsonObject jsonObject, @Nullable final Class<?> parentClass) {
Expand All @@ -39,11 +46,7 @@ public Class<?> identify(@Nonnull final JsonObject jsonObject, @Nullable final C
/** #microsoft.graph.user or #microsoft.graph.callrecords.callrecord */
final String odataType = jsonObject.get(ODATA_TYPE_KEY).getAsString();
final int lastDotIndex = odataType.lastIndexOf(".");
final String derivedType = (odataType.substring(0, lastDotIndex) +
".models." +
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL,
odataType.substring(lastDotIndex + 1)))
.replace("#", "com.");
final String derivedType = oDataTypeToClassName(odataType);
try {
Class<?> derivedClass = Class.forName(derivedType);
//Check that the derived class inherits from the given parent class
Expand All @@ -61,4 +64,20 @@ public Class<?> identify(@Nonnull final JsonObject jsonObject, @Nullable final C
//If there is no defined OData type, return null
return null;
}

/**
* Convert {@code @odata.type} to proper java class name
*
* @param odataType to convert
* @return converted class name
*/
@VisibleForTesting
static String oDataTypeToClassName(@Nonnull String odataType) {
Objects.requireNonNull(odataType);
final int lastDotIndex = odataType.lastIndexOf(".");
return (odataType.substring(0, lastDotIndex).toLowerCase(Locale.ROOT) +
".models." +
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, odataType.substring(lastDotIndex + 1)))
.replace("#", "com.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.microsoft.graph.serializer;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
* Created by Valentin Popov valentin@archiva.ru on 31.08.2021.
*/
class DerivedClassIdentifierTest {

@Test
void oDataTypeToClassName() {
Assertions.assertEquals("com.microsoft.graph.models.Message",
DerivedClassIdentifier.oDataTypeToClassName("#Microsoft.Graph.Message"));
}
}