microsoftgraph / msgraph-beta-sdk-java

Microsoft Graph Beta Java SDK
https://docs.microsoft.com/en-us/graph/sdks/use-beta?tabs=Java
MIT License
24 stars 8 forks source link

Cannot update sponsors of user #1002

Open Doc94 opened 1 month ago

Doc94 commented 1 month ago

Describe the bug

Im updating a user with new sponsors but in the update user throw an internal issue.

Expected behavior

Update the user with the sponsors provided

How to reproduce

Get a User class for existing user with sponsors, modify the sponsors list adding a new sponsors, set and update user.

SDK Version

6.13.0

Latest version known to work for scenario above?

No response

Known Workarounds

No response

Debug output

Click to expand log ``` Exception in thread "main" com.microsoft.graph.beta.models.odataerrors.ODataError: The context URL 'https://graph.microsoft.com/beta/$metadata#users(sponsors())/$entity' is invalid. at com.microsoft.graph.beta.models.odataerrors.ODataError.createFromDiscriminatorValue(ODataError.java:36) at com.microsoft.kiota.serialization.JsonParseNode.getObjectValue(JsonParseNode.java:212) at com.microsoft.kiota.http.OkHttpRequestAdapter.lambda$throwIfFailedResponse$0(OkHttpRequestAdapter.java:674) at com.microsoft.kiota.ApiExceptionBuilder.(ApiExceptionBuilder.java:26) at com.microsoft.kiota.http.OkHttpRequestAdapter.throwIfFailedResponse(OkHttpRequestAdapter.java:673) at com.microsoft.kiota.http.OkHttpRequestAdapter.send(OkHttpRequestAdapter.java:281) at com.microsoft.graph.beta.users.item.UserItemRequestBuilder.patch(UserItemRequestBuilder.java:1138) at com.microsoft.graph.beta.users.item.UserItemRequestBuilder.patch(UserItemRequestBuilder.java:1122) at xxx.MSEntraService.updateUser(MSEntraService.java:86) at xxx.Main.main(Main.java:147) ```

Configuration

Windows 11 x64

Other information

Any interaction with sponsors throw an error

Ndiritu commented 1 month ago

Hi @Doc94 The API supports different operations to assign/remove sponsors from a user. Please see: https://learn.microsoft.com/en-us/graph/api/user-post-sponsors?view=graph-rest-1.0&tabs=http https://learn.microsoft.com/en-us/graph/api/user-delete-sponsors?view=graph-rest-1.0&tabs=http

A work-around using the SDK:


User sponsor = new User();
sponsor.setId("");

graphClient.users().withUrl(
    client.getRequestAdapter().getBaseUrl() + "users/{userId}/sponsors/$ref"
).post(sponsor);

Let me know if this works

Doc94 commented 1 month ago

Hi @Doc94 The API supports different operations to assign/remove sponsors from a user. Please see: https://learn.microsoft.com/en-us/graph/api/user-post-sponsors?view=graph-rest-1.0&tabs=http https://learn.microsoft.com/en-us/graph/api/user-delete-sponsors?view=graph-rest-1.0&tabs=http

A work-around using the SDK:

User sponsor = new User();
sponsor.setId("");

graphClient.users().withUrl(
    client.getRequestAdapter().getBaseUrl() + "users/{userId}/sponsors/$ref"
).post(sponsor);

Let me know if this works

i test that and give me an error.

Exception in thread "main" com.microsoft.graph.beta.models.odataerrors.ODataError: An unexpected 'EndOfInput' node was found when reading from the JSON reader. A 'StartObject' node was expected.
    at com.microsoft.graph.beta.models.odataerrors.ODataError.createFromDiscriminatorValue(ODataError.java:36)
    at com.microsoft.kiota.serialization.JsonParseNode.getObjectValue(JsonParseNode.java:212)
    at com.microsoft.kiota.http.OkHttpRequestAdapter.lambda$throwIfFailedResponse$0(OkHttpRequestAdapter.java:674)
    at com.microsoft.kiota.ApiExceptionBuilder.<init>(ApiExceptionBuilder.java:26)
    at com.microsoft.kiota.http.OkHttpRequestAdapter.throwIfFailedResponse(OkHttpRequestAdapter.java:673)
    at com.microsoft.kiota.http.OkHttpRequestAdapter.send(OkHttpRequestAdapter.java:281)
    at com.microsoft.graph.beta.users.UsersRequestBuilder.post(UsersRequestBuilder.java:165)
    at com.microsoft.graph.beta.users.UsersRequestBuilder.post(UsersRequestBuilder.java:149)

i test this based in another methods (not the perfect code...)

public void addSponsor(@NotNull String userId, @NotNull String sponsorId) {
        Preconditions.checkArgument(true, "Group ID cannot be null");
        com.microsoft.graph.beta.models.ReferenceCreate referenceCreate = new com.microsoft.graph.beta.models.ReferenceCreate();
        referenceCreate.setOdataId("https://graph.microsoft.com/v1.0/users/" + sponsorId);

        HashMap<String, Object> requestParameters = new HashMap<>();
        requestParameters.put("baseurl", "https://graph.microsoft.com/beta");
        requestParameters.put("user%2Did", userId);
        RequestInformation requestInformation = new RequestInformation(HttpMethod.POST, "{+baseurl}/users/{user%2Did}/sponsors/$ref", requestParameters);
        requestInformation.headers.tryAdd("Accept", "application/json");
        requestInformation.setContentFromParsable(this.graphClient.getRequestAdapter(), "application/json", referenceCreate);

        HashMap<String, ParsableFactory<? extends Parsable>> errorMapping = new HashMap();
        errorMapping.put("XXX", ODataError::createFromDiscriminatorValue);
        this.graphClient.getRequestAdapter().sendPrimitive(requestInformation, errorMapping, Void.class);
    }

and this works but pretty sure im make unnecesary things for that.