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.96k stars 6.59k forks source link

[BUG] [Dart] Documentation for auth uses defaultApiClient.getAuthentication<ApiKeyAuth> but no such methods exists #12374

Open 0xNF opened 2 years ago

0xNF commented 2 years ago
Description

See: https://github.com/OpenAPITools/openapi-generator/blob/df05e6f4bc6265ba4de801ba306f75dd7278e119/samples/openapi3/client/petstore/dart2/petstore_client_lib/doc/UserApi.md

import 'package:openapi/api.dart';
// TODO Configure API key authorization: api_key
//defaultApiClient.getAuthentication<ApiKeyAuth>('api_key').apiKey = 'YOUR_API_KEY';
// uncomment below to setup prefix (e.g. Bearer) for API key, if needed
//defaultApiClient.getAuthentication<ApiKeyAuth>('api_key').apiKeyPrefix = 'Bearer';

But the ApiClient class has no such method. Here are the available methods on the object:

invokeAPI()
addDefaultHeader()
deserializeAsync()
serializeAsync()
deserialize()
serialize()

There's a .authentication object but the interface only has a single void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams); function.

Consequently, I don't know what the canonical way to do auth, and specifically Cookie Auth is.

openapi-generator version

commit 559a2618dfe62dc8b86d0eb169bb4d998774a21f, Sunday May 15 2022

vitor-soares-iteam commented 2 years ago

Facing the same issue.

This method used to exist, but since we've updated to the newer version with dart null-safety its not there anymore. We are not able to update the auth token anymore. Any one got a workaround for this?

kaz080 commented 2 years ago

@0xNF In my case, HttpBearerAuth is working fine with this code: (openapi-generator-cli 6.0.1)

  var authentication = HttpBearerAuth();
  authentication.accessToken = initialToken;
  var client = ApiClient(
    basePath: 'https://.../api/v2',
    authentication: authentication,
  );
  final api = SystemApi(client);

ApiKeyAuth seems supporting cookie. The code will be like this:

  var authentication = ApiKeyAuth('cookie', 'paramName');
  authentication.apiKey = 'YOUR_API_KEY';
  // authentication.apiKeyPrefix = 'Bearer';
  var client = ApiClient(
    authentication: authentication,
  );
  final api = UserApi(client);
kaz080 commented 2 years ago

ApiKeyAuth implementation: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/dart2/auth/api_key_auth.mustache