Skip to content

[Fix #3052] Add support for optional query parameters#3250

Closed
jonahgeorge wants to merge 2 commits intoOpenAPITools:masterfrom
jonahgeorge:fix-3052
Closed

[Fix #3052] Add support for optional query parameters#3250
jonahgeorge wants to merge 2 commits intoOpenAPITools:masterfrom
jonahgeorge:fix-3052

Conversation

@jonahgeorge
Copy link

@jonahgeorge jonahgeorge commented Jun 30, 2019

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.
  • Filed the PR against the correct branch: master, 4.1.x, 5.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

Addresses #3052.

This adds a new feature flag for the two Rust clients named generateOptionQueryParams which when enabled wraps any non-required query parameters in Option<T> and does not add them to the request if None.

@frol @farcaller @bjgill

@jonahgeorge
Copy link
Author

jonahgeorge commented Jun 30, 2019

fn update_pet_with_form(&self, pet_id: i64, name: Option<&str>, status: Option<&str>) -> Result<(), Error> {
    let configuration: &configuration::Configuration = self.configuration.borrow();
    let client = &configuration.client;

    let uri_str = format!("{}/pet/{petId}", configuration.base_path, petId=pet_id);
    let mut req_builder = client.post(uri_str.as_str());

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(ref token) = configuration.oauth_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };
    let mut form_params = std::collections::HashMap::new();
    form_params.insert("name", name.to_string());
    form_params.insert("status", status.to_string());
    req_builder = req_builder.form(&form_params);

Doing some more testing, it looks like this has issues due to the Option on the form parameters. Curious if anyone has input on a better way to address this: seems like I could either add the same changes to the form parameters as well or break the operation parameters into queryParams and formParams.

@bcourtine
Copy link
Contributor

bcourtine commented Jul 10, 2019

Hi @jonahgeorge,

IMO, this PR is a must have. I already forked it for one of my projects and it gave expected results.
Thanks a lot.

I think you forgot to regenerate OpenAPI generator general doocumentation, which explain the CI failure for your PR. The new parameter have to be added to this documentation (rust.md file).

@wing328
Copy link
Member

wing328 commented Jul 12, 2019

@jonahgeorge thanks for the PR.

Instead of introducing a new option, what about using nullable instead? We've done that in some other clients, e.g.

{{^isNullable}}
{{#required}}
# verify the required parameter '{{paramName}}' is set
if @api_client.config.client_side_validation && {{{paramName}}}.nil?
fail ArgumentError, "Missing the required parameter '{{paramName}}' when calling {{classname}}.{{operationId}}"
end
{{#isEnum}}
{{^isContainer}}
# verify enum value
allowable_values = [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]
if @api_client.config.client_side_validation && !allowable_values.include?({{{paramName}}})
fail ArgumentError, "invalid value for \"{{{paramName}}}\", must be one of #{allowable_values}"
end
{{/isContainer}}
{{/isEnum}}
{{/required}}
{{/isNullable}}

@jonahgeorge
Copy link
Author

@wing328 Agree that using nullable is a better approach. I wasn't aware that variable is exposed to the templates (besides looking at other languages, is there an easy way to see what is passed in?).

Also, wouldn't it still be considered a breaking change to make nullable=true fields use Option<T> if they aren't right now and require a feature flag?

@wing328
Copy link
Member

wing328 commented Jul 13, 2019

Agree that using nullable is a better approach. I wasn't aware that variable is exposed to the templates (besides looking at other languages, is there an easy way to see what is passed in?).

You can use -DdebugOperations, which will show its value (boolean)

Also, wouldn't it still be considered a breaking change to make nullable=true fields use Option if they aren't right now and require a feature flag?

To me, it's more like correcting the behavior. For other languages/generators, we do not consider it as a breaking change but of course, I like your suggestion to give a proper heads-up to the user and how they can use nullable (OAS3) or x-nullable (OAS2) to control the behavior.

@bcourtine
Copy link
Contributor

Any news for this PR?

If nobody is working on it anymore, I can try to fix it…

@wing328
Copy link
Member

wing328 commented Oct 1, 2019

@bcourtine please kindly go ahead. What about merging it into a branch (in this project) so that more people can work on it?

bcourtine added a commit to bcourtine/openapi-generator that referenced this pull request Oct 1, 2019
As stated in OpenAPITools#3250

* Making this optinal gives more complicated templates, harder to maintain.
* Such breaking change is acceptable, since it's more a behavior correction.
@jonahgeorge
Copy link
Author

Sorry for the radio silence, I haven't had time to polish this up.
@bcourtine if you want to take it over, go for it

@bcourtine
Copy link
Contributor

Hello @wing328 @jonahgeorge

I completed this PR and proposed a new one here: #4016

IMO, it should be mergeable (hyper/reqwest sample code compiles without error).

@bcourtine
Copy link
Contributor

PR #4016 (which includes and completes this one) has been merged. So this PR can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants