OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.28k stars 6.44k forks source link

[BUG] Differences with Rust and Python generators #12163

Open tschuett opened 2 years ago

tschuett commented 2 years ago

Bug Report Checklist

Description

The OpenAPI definition is at: https://github.com/SchedMD/slurm/tree/master/src/plugins/openapi/v0.0.38

pub struct Configuration {
    pub base_path: String,
    pub user_agent: Option<String>,
    pub client: reqwest::Client,
    pub basic_auth: Option<BasicAuth>,
    pub oauth_access_token: Option<String>,
    pub bearer_access_token: Option<String>,
    pub api_key: Option<ApiKey>,
    // TODO: take an oauth2 token source, similar to the go one
}

pub struct ApiKey {
    pub prefix: Option<String>,
    pub key: String,
}
class Configuration(object):
    """NOTE: This class is auto generated by OpenAPI Generator

    Ref: https://openapi-generator.tech
    Do not edit the class manually.

    :param host: Base url
    :param api_key: Dict to store API key(s).
      Each entry in the dict specifies an API key.
      The dict key is the name of the security scheme in the OAS specification.
      The dict value is the API key secret.
    :param api_key_prefix: Dict to store API prefix (e.g. Bearer)
      The dict key is the name of the security scheme in the OAS specification.
      The dict value is an API key prefix when generating the auth data.
 def auth_settings(self):
        """Gets Auth Settings dict for api client.

        :return: The Auth Settings information dict.
        """
        auth = {}
        if 'token' in self.api_key:
            auth['token'] = {
                'type': 'api_key',
                'in': 'header',
                'key': 'X-SLURM-USER-TOKEN',
                'value': self.get_api_key_with_prefix(
                    'token',
                ),
            }
        if 'user' in self.api_key:
            auth['user'] = {
                'type': 'api_key',
                'in': 'header',
                'key': 'X-SLURM-USER-NAME',
                'value': self.get_api_key_with_prefix(
                    'user',
                ),
            }
        return auth

I believe the Python version is correct. Note that I have to set two api keys: token and user.

openapi-generator version

5.4.0 from Homebrew

OpenAPI declaration file content or url
Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix

Note that for Rust the ApiKey is one optional value. For Python it is two dictionaries.

tschuett commented 2 years ago
    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
        let local_var_key = local_var_apikey.key.clone();
        let local_var_value = match local_var_apikey.prefix {
            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
            None => local_var_key,
        };
        local_var_req_builder = local_var_req_builder.header("X-SLURM-USER-TOKEN", local_var_value);
    };
    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
        let local_var_key = local_var_apikey.key.clone();
        let local_var_value = match local_var_apikey.prefix {
            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
            None => local_var_key,
        };
        local_var_req_builder = local_var_req_builder.header("X-SLURM-USER-NAME", local_var_value);
    };

This looks problematic. It reads the api key twice.

wing328 commented 2 years ago

can you try v6.0.0-beta or the latest master ? if i remember correctly, there's a bug fix related to authentication.

tschuett commented 2 years ago

I believe the problem is here: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/rust/reqwest/configuration.mustache The Configuration struct has one optional ApiKey and not two HashMaps.

wing328 commented 2 years ago

ok, can you please file a PR with a fix using HashMap instead?

tschuett commented 2 years ago

This is way out of my area of expertise. But I believe there is also generated code accessing the api keys, see above. The code should have looked into dictionaries (user and token).

See the python code for reference.

tschuett commented 2 years ago

The generated code seems to be here: https://github.com/OpenAPITools/openapi-generator/blob/6a77660b08eb16d1d1ed577778b11fe46219b559/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache#L132 BUT I do not anything about mustache.

tschuett commented 2 years ago

Friendly ping. Are there any plans to address the problem?