-
Notifications
You must be signed in to change notification settings - Fork 219
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
Handle required query string and headers in Java #4421
Comments
Hi @piotrooo First off, around nullability, I can see that you've already submitted a PR, thanks! microsoft/kiota-java#1149 Then, here are a couple of answers to your questions. The headers are not being projected to their own type due to a mix of a size concern and a concern about the developer experience of an object that'd be mixed between properties and a map behaviour. Both the examples of headers you've provided look like they are cross-cutting (required for all or a majority of operations on the API surface). For that, instead of specifying things on a per request base we suggest:
Besides avoiding duplication of code, this approach will allow that initialization to live where your application configuration/initialization happens, separate from where the requests are made. Let us know if you have further questions. |
I'm not sure if I understand you correctly, but creating manually a middleware per request is not what you want from a code generator. The variable nature of the OpenAPI spec disables the generation of reusable code. Let's consider the following scenario:
And so on for other endpoints with different, sets of request-defined headers. This could be described using the following OpenAPI Spec. OpenAPI Spec
Creating middleware is really inconvenient. If I have to manually create some parts of the OpenAPI Spec, why would I need a generator? I can do this manually. |
in that scenario, yes providing headers at request time makes sense. |
Could you elaborate on it? Or show an example? I don't understand the purpose of having two clients; it seems to be very odd to me. I want to simplify things, not complicate them further with another client. This is a reason why I want to use a generator: to simplify things. Moreover, I have all the necessary information in the OpenAPI Spec. If you consider the nature of the defined headers in the OpenAPI Spec, you can easily compare them to query parameters. Even though the section is labeled I play around a little bit with the desired (?) API for request configuration. Couple of ideas: 1. Like
|
Thank you for the suggestion.
Usually, cross cutting concerns like telemetry, authentication, etc... are better handled as an authentication provider and/or a middleware handler as I explained in a previous reply.
You could generate two different clients:
This way the auto-completion will ONLY suggest operations that are valid for the scenario, and you don't need to provide the headers on every request. I hope that makes things clearer? |
@baywet thanks for answering. I believe this conversation could produce something good 👍. That's a constructive flame 😉
My example was just an idea. Of course, we can discuss the desired API. Perhaps we could include a discriminator header/query to methods, or maybe add another nesting level. Other users might have preferences, so we could ask them as well.
Yes, maybe configurers should have a base class with that kind of "utility" methods. I like this idea.
Now I understand (I hope) what you mean by using middleware. Middleware for fixed values is ok (e.g., Authentication, Trace-Id, etc). The described case with the Moreover, if something is in the OpenAPI Spec (headers, cookies, query parameters), it should be reflected in the generated code. |
The other challenge with this approach (not using a lambda/callback for the configuration) is it requires to add an additional import. We've already received the feedback this can be challenging to users with deeply nested APIs. |
It's too broad. I don't know how to respond to that. Perhaps I should ask - @baywet, what other information do you need? I believe I've provided you with a specific case, benefits, and an example of how it could look. I don't have any further arguments. |
I don't need additional information at this point. Thanks for the open discussion. |
+1 from me, this was slightly related: #2428 but the proposed builder pattern, e.g.: TemporaryFileResponse temporaryFileResponse = sampleClient
.api()
.temporaryFiles()
.get(temporaryFilesRequestConfiguration()
.headerOne("value1")
.xClient("value2")
.token("value3")
); Looks extremely appealing! |
I think we should consider this for 2.0 and across languages! |
Note
First of all, many thanks for your work on the library. It could be very important for a lot of projects! 🙏
Please notice one huge disclaimer when I wrote this issue.
Important
The purpose of generating API clients is so that you can provide them to completely different teams, and they should be as intuitive as possible for them to use - even if they don't know exactly how to use it. So in some situations - generated API clients - must 'think' for the API consumers.
Let's say we have the following OpenAPI definition:
we have one required header (
X-Client
) and one required query (token
).Right now I'm consuming this API in that way:
A couple of improvements could be done here (*):
(*) all examples are cleaned for unnecessary code for brevity
@Nullable
Definition of the
GetQueryParameters
is:That definition of the query parameter isn't valid because it is required.
Headers defined in OpenAPI Spec should be available as query parameters for auto-completion purposes and to help better understand how the API works.
Using following example:
queryParameters
shouldn't be@Nullable
Instead of this the empty
QueryParameters
object should be used.This helps with following warnings:
When the query and the header are required, there should be an additional check before serialization to ensure that they aren't
null
.I hope 🙏 these ideas make sense for you. If something needs more clarification - let me know.
The text was updated successfully, but these errors were encountered: