microsoftgraph / microsoftgraph/msgraph-sdk-java

SDK NullPointerException due to missing @odata.type field during select query

未关闭
#2,587 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

status:waiting-for-triage type:bug
主要语言
Java
星标
444
派生
154
平均合并
18 小时 28 分钟
30 天内合并 PR
4

描述

Describe the bug

During a select query for users, mapping them to the User class fails with following NPE

java.lang.NullPointerException: parameter node cannot be null
	at java.util.Objects.requireNonNull(Objects.java:246)
	at io.kiota.serialization.json.JsonParseNode.<init>(JsonParseNode.java:38)
	at io.kiota.serialization.json.JsonParseNodeFactory.createJsonParseNode(JsonParseNodeFactory.java:32)
	at io.kiota.serialization.json.JsonParseNode.getChildNode(JsonParseNode.java:48)
	at com.microsoft.graph.models.User.createFromDiscriminatorValue(User.java:30)
	at io.kiota.serialization.json.JsonParseNode.getObjectValue(JsonParseNode.java:248)
	at io.kiota.serialization.json.JsonParseNode.getCollectionOfObjectValues(JsonParseNode.java:219)
	at com.microsoft.graph.models.UserCollectionResponse.lambda$getFieldDeserializers$0(UserCollectionResponse.java:34)
	at io.kiota.serialization.json.JsonParseNode.assignFieldValues(JsonParseNode.java:301)
	at io.kiota.serialization.json.JsonParseNode.getObjectValue(JsonParseNode.java:249)
	at com.microsoft.kiota.http.OkHttpRequestAdapter.send(OkHttpRequestAdapter.java:322)
	at com.microsoft.graph.groups.item.members.graphuser.GraphUserRequestBuilder.get(GraphUserRequestBuilder.java:70)
Expected behavior

The user(s) should be selected by the query, mapped and returned in a response object

How to reproduce

Code snippet to reproduce:

final String groupId = "test group id";
final GraphUserRequestBuilder request = client.groups()
                .byGroupId(groupId)
                .members()
                .graphUser()
                .get(req -> {
                    req.queryParameters.select = "id,name,mail";
                    req.queryParameters.filter = "id in (1,2,3)";
                    req.queryParameters.count = true;
                    req.headers.add("ConsistencyLevel", "eventual");
                    }
                })

Code snippet to reproduce the NPE with the JsonNode

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.kiota.serialization.json.JsonParseNode;
import io.kiota.serialization.json.JsonParseNodeFactory;

class Scratch {
    static void main() {
        try {
            var a = new JsonParseNode(new JsonParseNodeFactory(), new ObjectNode(new JsonNodeFactory(false)));
            var b = a.getChildNode("@odata.type");
        } catch (NullPointerException e) {
            System.out.println("Exception!");
        }
    }
}

Replicating this code in Graph UI returns following example response:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(id,name,mail)",
    "@odata.count": 2,
    "value": [
        {
            "id": "id 1",
            "name": "name 1",
            "mail": "mail 1"
        },
        {
            "id": "id 2",
            "name": "name 2",
            "mail": "mail 2"
        }
    ]
}

Which then leads to the NPE as the SDK is expecting @odata.type field being set here

https://github.com/microsoftgraph/msgraph-sdk-java/blob/main/src/main/java/com/microsoft/graph/generated/models/User.java

@jakarta.annotation.Nonnull
    public static User createFromDiscriminatorValue(@jakarta.annotation.Nonnull final ParseNode parseNode) {
        Objects.requireNonNull(parseNode);
        final ParseNode mappingValueNode = parseNode.getChildNode("@odata.type"); <- NPE being caused here
        if (mappingValueNode != null) {
            final String mappingValue = mappingValueNode.getStringValue();
            switch (mappingValue) {
                case "#microsoft.graph.agentUser": return new AgentUser();
            }
        }
        return new User();
    }
SDK Version

6.63.0

Latest version known to work for scenario above?

6.62.0

Known Workarounds

No response

Debug output
Click to expand log ```
</details>


### Configuration

_No response_

### Other information

_No response_

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 src/main/java/com/microsoft/graph/generated/models/User.java 中的 User.createFromDiscriminatorValue 开始,跟踪缺失的 @odata.type 字段对应的 JsonParseNode.getChildNode。使用提供的 select 查询或 JsonNode 片段重现该问题,然后验证仅包含 id、name 和 mail 的用户能够在不发生 NullPointerException 的情况下完成映射并返回。

由索引模型根据 Issue 内容生成。

评估

技术栈
java
领域
api
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
68/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。