Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not override user's settings from config #3401

Merged
merged 17 commits into from
Oct 17, 2023
Merged

Conversation

andreaTP
Copy link
Contributor

@andreaTP andreaTP commented Oct 2, 2023

Fixes #3378 for Java and C# as a start, if we agree that this is the desired encoding I'll review and propagate the changes to the rest of the languages.

This is a sample result for C#:

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
#nullable enable
public async Task<double?> MethodName(string b, string c, Action<RequestConfig>? config = default) {
#nullable restore
#else
public async Task<double?> MethodName(string b, string c, Action<RequestConfig> config = default) {
#endif
    if(string.IsNullOrEmpty(b)) throw new ArgumentNullException(nameof(b));
    if(string.IsNullOrEmpty(c)) throw new ArgumentNullException(nameof(c));
    var requestInfo = new RequestInformation {
        HttpMethod = Method.GET,
        UrlTemplate = UrlTemplate,
        PathParameters = PathParameters,
    };
    if (config != null) {
        var requestConfig = new RequestConfig();
        config.Invoke(requestConfig);
        requestInfo.AddQueryParameters(requestConfig.QueryParameters);
        requestInfo.AddRequestOptions(requestConfig.Options);
        requestInfo.AddHeaders(requestConfig.Headers);
    }
    if (!requestInfo.Headers.ContainsKey("Accept")) {
        requestInfo.Headers.Add("Accept", "application/json");
    }
    if (!requestInfo.Headers.ContainsKey("Content-Type")) {
        requestInfo.SetContentFromScalar(RequestAdapter, "", b);
    }
    return requestInfo;
}

@andreaTP andreaTP requested a review from a team as a code owner October 2, 2023 14:42
@andreaTP andreaTP marked this pull request as draft October 2, 2023 14:42
Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for starting this. To reduce the amount of code we're projecting, I'm suggesting to add TryAdd methods in the headers classes in abstractions. Thoughts?

@andreaTP
Copy link
Contributor Author

andreaTP commented Oct 2, 2023

To reduce the amount of code we're projecting, I'm suggesting to add TryAdd methods in the headers classes in abstractions.

This change is going to be not retro compatible and would require, at minimum a minor version bump for the GA languages.

Thoughts?

If we decide to go down this path I'd propose we go full-in and refactor the entire logic into abstractions, something like:

generated:

public async Task<double?> MethodName(string b, string c, Action<RequestConfig> config = default) {
#endif
    if(string.IsNullOrEmpty(b)) throw new ArgumentNullException(nameof(b));
    if(string.IsNullOrEmpty(c)) throw new ArgumentNullException(nameof(c));
    var requestInfo = new RequestInformation {
        HttpMethod = Method.GET,
        UrlTemplate = UrlTemplate,
        PathParameters = PathParameters,
    };
    applyUserConfig(requestInfo, config);
    return requestInfo;
}

library (in abstractions):

public async void applyUserConfig(RequestInfo requestInfo, Action<RequestConfig> config) {
    if (config != null) {
        var requestConfig = new RequestConfig();
        config.Invoke(requestConfig);
        requestInfo.AddQueryParameters(requestConfig.QueryParameters);
        requestInfo.AddRequestOptions(requestConfig.Options);
        requestInfo.AddHeaders(requestConfig.Headers);
    }
    if (!requestInfo.Headers.ContainsKey("Accept")) {
        requestInfo.Headers.Add("Accept", "application/json");
    }
    if (!requestInfo.Headers.ContainsKey("Content-Type")) {
        requestInfo.SetContentFromScalar(RequestAdapter, "", b);
    }
}

wdyt?

@andreaTP
Copy link
Contributor Author

andreaTP commented Oct 2, 2023

oh, yes, if we go and refactor the entire thing, adding a TryAdd method is fine 👍

@baywet
Copy link
Member

baywet commented Oct 2, 2023

Adding a try add method: We expect users to update dependencies to what the kiota info command indicates as they refresh clients. So if we ship a generation change that requires additional API surface in kiota abstractions, it's not considered a breaking change.

Apply user configuration method: that's a nice idea. The only issues with that:

  • The type of the request config varies (so calling new will be impossible in some languages)
  • The type of config (the callback) also varies accordingly.
  • Some request configuration classes do not have a query parameter property (no documented query parameters)
  • Not all methods send a request body (content type header)

For all those reasons, we had originally left this as generated code instead of wrapping everything in the abstractions.

@andreaTP
Copy link
Contributor Author

andreaTP commented Oct 3, 2023

Thanks for the context @baywet , make sense.
I'll add the tryAdd method to all of the abstractions and make the changes for this PR.

@andreaTP
Copy link
Contributor Author

I need to get microsoft/kiota-abstractions-php#91 merged and released to finish this one 😓

it/java/basic/pom.xml Outdated Show resolved Hide resolved
it/go/go.mod Show resolved Hide resolved
@baywet
Copy link
Member

baywet commented Oct 12, 2023

I need to get microsoft/kiota-abstractions-php#91 merged and released to finish this one 😓

I just did release 0.8.4 a few minutes ago, you should be good to go. Let me know if you need anything else.

@andreaTP
Copy link
Contributor Author

andreaTP commented Oct 12, 2023

This is now ready for review (IT tests are passing on my fork).
I think we have an unrelated regression though: #3401 (comment)

@andreaTP andreaTP marked this pull request as ready for review October 12, 2023 15:31
baywet
baywet previously approved these changes Oct 16, 2023
@baywet
Copy link
Member

baywet commented Oct 16, 2023

@andreaTP I think we're almost there. Can you add a changelog entry please?

@andreaTP
Copy link
Contributor Author

sure done 👍

baywet
baywet previously approved these changes Oct 16, 2023
@baywet baywet merged commit 04d143d into microsoft:main Oct 17, 2023
170 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Java - RequestConfiguration gets applied to empty headers and options
2 participants