microsoftgraph / msgraph-sdk-java

Microsoft Graph SDK for Java
https://docs.microsoft.com/en-us/graph/sdks/sdks-overview
MIT License
387 stars 132 forks source link

Serialization error of objects in Graph v6.1 #1798

Open sa-obgit-backend opened 8 months ago

sa-obgit-backend commented 8 months ago

Serialization of an Graph Drive will lead to an error:

15:31:32.872 [Test worker] WARN de.baudc.connector.microsoft.graph.adapter.out.GraphDriveConverter -- Could not serialize graphDrive: com.microsoft.graph.models.Drive@70b630d
java.lang.IllegalStateException: Dangling name: group
    at com.google.gson.stream.JsonWriter.close(JsonWriter.java:346)
    at com.google.gson.stream.JsonWriter.endObject(JsonWriter.java:321)
    at com.microsoft.kiota.serialization.JsonSerializationWriter.writeObjectValue(JsonSerializationWriter.java:307)
    at com.microsoft.graph.models.Drive.serialize(Drive.java:148)
    at com.microsoft.kiota.serialization.JsonSerializationWriter.writeObjectValue(JsonSerializationWriter.java:292)
    at de.baudc.connector.microsoft.graph.adapter.out.GraphDriveConverter.convert(GraphDriveConverter.java:48)
    at java.base/java.util.Optional.map(Optional.java:260)
    at de.baudc.connector.microsoft.graph.adapter.out.GraphDriveRepository.getRootDrive(GraphDriveRepository.java:68)
    at de.baudc.connector.microsoft.graph.adapter.out.GraphDriveRepositoryTest.getRootDrive(GraphDriveRepositoryTest.java:219)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:728)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
    at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103)
    at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
    at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:92)
    at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:86)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:218)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:214)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:139)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:119)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:94)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:89)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:62)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
    at jdk.proxy2/jdk.proxy2.$Proxy5.stop(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker$3.run(TestWorker.java:193)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
    at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
    at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:113)
    at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:65)
    at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
    at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)
    Suppressed: java.io.IOException: Incomplete document
        at com.google.gson.stream.JsonWriter.close(JsonWriter.java:610)
        at com.microsoft.kiota.serialization.JsonSerializationWriter.close(JsonSerializationWriter.java:371)
        at de.baudc.connector.microsoft.graph.adapter.out.GraphDriveConverter.convert(GraphDriveConverter.java:47)
        ... 83 common frames omitted

I think because of the Code in Drive:

public void serialize(@Nonnull final SerializationWriter writer) {
        Objects.requireNonNull(writer);
        super.serialize(writer);
        writer.writeCollectionOfObjectValues("bundles", this.getBundles());
        writer.writeStringValue("driveType", this.getDriveType());
        writer.writeCollectionOfObjectValues("following", this.getFollowing());
        writer.writeCollectionOfObjectValues("items", this.getItems());
        writer.writeObjectValue("list", this.getList(), new Parsable[0]);
        writer.writeObjectValue("owner", this.getOwner(), new Parsable[0]);
        writer.writeObjectValue("quota", this.getQuota(), new Parsable[0]);
        writer.writeObjectValue("root", this.getRoot(), new Parsable[0]);
        writer.writeObjectValue("sharePointIds", this.getSharePointIds(), new Parsable[0]);
        writer.writeCollectionOfObjectValues("special", this.getSpecial());
        writer.writeObjectValue("system", this.getSystem(), new Parsable[0]);
    }

There is an owner defined and linked to IdentitySet but in IdentitySet:

public void serialize(@Nonnull final SerializationWriter writer) {
        Objects.requireNonNull(writer);
        writer.writeObjectValue("application", this.getApplication(), new Parsable[0]);
        writer.writeObjectValue("device", this.getDevice(), new Parsable[0]);
        writer.writeStringValue("@odata.type", this.getOdataType());
        writer.writeObjectValue("user", this.getUser(), new Parsable[0]);
        writer.writeAdditionalData(this.getAdditionalData());
    }

There is no "group" -> error

Perhaps there are more missing "IdentitySet" object, but I think at least group is missing

Expected behavior

The serialization works and gives JSON from the Microsoft object

Actual behavior

IllegalStateException at serialization

Steps to reproduce the behavior

Map<String, Object> raw = null;
        try (final SerializationWriter writer = new JsonSerializationWriter()) {
            writer.writeObjectValue("", graphDrive);
            try (final InputStream stream = writer.getSerializedContent()) {
                String json = new String(Compatibility.readAllBytes(stream), StandardCharsets.UTF_8);
                raw = Map.of(GRAPH_DRIVE, json);
            }
        } catch (IOException | RuntimeException e) {
            log.warn("Could not serialize graphDrive: {}", graphDrive, e);
        }

will explode at

writer.writeObjectValue("", graphDrive);
baywet commented 8 months ago

Hi @sa-obgit-backend Thanks for using the Java SDK and for reaching out. Can you also share how you initialize the Drive object please? (anonymize any data)

sa-obgit-backend commented 8 months ago

Yes sure, nothing special here.

GET direct via CURL/Postman against official Graph endpoint. Then you get a JSON like

{
                    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives/$entity",
                    "createdDateTime": "2021-09-19T00:09:39Z",
                    "description": "xxx",
                    "id": "xxx",
                    ...
                    "createdBy": {
                        "user": {
                            "displayName": "xxx"
                        }
                    },
                    "lastModifiedBy": {
                        "user": {
                            "email": "xxx",
                            "id": "xxx",
                            "displayName": "xxx"
                        }
                    },
                    "owner": {
                        "group": {
                            "email": "xxx",
                            "id": "xxx",
                            "displayName": "xxx"
                        }
                    },
                    "quota": {
                        ...
                    }
                }

and then you can convert to a drive object via

ParseNode driveParseNode =
                new JsonParseNodeFactory().getParseNode("application/json", new ByteArrayInputStream(responseString.getBytes(StandardCharsets.UTF_8)));
        com.microsoft.graph.models.Drive graphDrive = driveParseNode.getObjectValue(com.microsoft.graph.models.Drive::createFromDiscriminatorValue);

Then you have conversion unit test that principally works directionally (normally if no error occurs)