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

[BUG][DIO][DIO-NEXT] UNKNOWN_BASE_TYPE with direct requestBody structure #12140

Closed
5 of 6 tasks
mrdev023 opened this issue Apr 14, 2022 · 1 comment
Closed
5 of 6 tasks

Comments

@mrdev023
Copy link

mrdev023 commented Apr 14, 2022

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When i set schema directly, it returns UNKNOWN_BASE_TYPE model but if i create components/schema and use ref it works.

openapi-generator version
<dependency>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-cli</artifactId>
            <version>5.3.0</version>
</dependency>

From https://github.com/gibahjoe/openapi-generator-dart/blob/3.3.0/openapi-generator-cli/pom.xml

OpenAPI declaration file content or url
openapi: 3.0.1
info: [...]
security:
  - BearerToken: []
paths:
  [...]
  /api/users/password:
    post:
      summary: Send instruction
      description: Send token to update password
      operationId: sendPasswordToken
      tags:
        - Password
      requestBody:
        content:
          application/json:
            schema:
              properties:
                email:
                  type: string
                  format: email
      responses:
        '200':
          description: Reset instructions send by email
          content:
            application/json:
              schema:
                required:
                  - response
                properties:
                  response:
                    type: string
              example:
                response: Reset password instructions send by mail
        '404':
          description: User not found
          content:
            application/json:
              schema:
                required:
                  - response
                properties:
                  response:
                    type: string
              example:
                response: User not found
  [...]
components:
  securitySchemes:
    BearerToken:
      type: http
      scheme: bearer
      bearerFormat: JWT
Generation Details
/// Send instruction
  /// Send token to update password
  ///
  /// Parameters:
  /// * [UNKNOWN_BASE_TYPE] 
  /// * [cancelToken] - A [CancelToken] that can be used to cancel the operation
  /// * [headers] - Can be used to add additional headers to the request
  /// * [extras] - Can be used to add flags to the request
  /// * [validateStatus] - A [ValidateStatus] callback that can be used to determine request success based on the HTTP status of the response
  /// * [onSendProgress] - A [ProgressCallback] that can be used to get the send progress
  /// * [onReceiveProgress] - A [ProgressCallback] that can be used to get the receive progress
  ///
  /// Returns a [Future] containing a [Response] with a [JsonObject] as data
  /// Throws [DioError] if API call or serialization fails
  Future<Response<JsonObject>> sendPasswordToken({ 
    UNKNOWN_BASE_TYPE? UNKNOWN_BASE_TYPE,
    CancelToken? cancelToken,
    Map<String, dynamic>? headers,
    Map<String, dynamic>? extra,
    ValidateStatus? validateStatus,
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
  }) async {
    final _path = r'/api/users/password';
    final _options = Options(
      method: r'POST',
      headers: <String, dynamic>{
        ...?headers,
      },
      extra: <String, dynamic>{
        'secure': <Map<String, String>>[
          {
            'type': 'http',
            'scheme': 'bearer',
            'name': 'BearerToken',
          },
        ],
        ...?extra,
      },
      contentType: 'application/json',
      validateStatus: validateStatus,
    );

    dynamic _bodyData;

    try {
      const _type = FullType(UNKNOWN_BASE_TYPE);
      _bodyData = UNKNOWN_BASE_TYPE == null ? null : _serializers.serialize(UNKNOWN_BASE_TYPE, specifiedType: _type);

    } catch(error, stackTrace) {
      throw DioError(
         requestOptions: _options.compose(
          _dio.options,
          _path,
        ),
        type: DioErrorType.other,
        error: error,
      )..stackTrace = stackTrace;
    }

    final _response = await _dio.request<Object>(
      _path,
      data: _bodyData,
      options: _options,
      cancelToken: cancelToken,
      onSendProgress: onSendProgress,
      onReceiveProgress: onReceiveProgress,
    );

    JsonObject _responseData;

    try {
      const _responseType = FullType(JsonObject);
      _responseData = _serializers.deserialize(
        _response.data!,
        specifiedType: _responseType,
      ) as JsonObject;

    } catch (error, stackTrace) {
      throw DioError(
        requestOptions: _response.requestOptions,
        response: _response,
        type: DioErrorType.other,
        error: error,
      )..stackTrace = stackTrace;
    }

    return Response<JsonObject>(
      data: _responseData,
      headers: _response.headers,
      isRedirect: _response.isRedirect,
      requestOptions: _response.requestOptions,
      redirects: _response.redirects,
      statusCode: _response.statusCode,
      statusMessage: _response.statusMessage,
      extra: _response.extra,
    );
  }
Steps to reproduce
@Openapi(
    additionalProperties:
        DioProperties(pubAuthor: '[...]', pubName: '[...]_api'),
    alwaysRun: true,
    inputSpecFile: 'http://localhost:3000/apidocs.yaml',
    generatorName: Generator.dioNext,
    // generatorName: Generator.dio, // Same result
    outputDirectory: '[...]_api',
    typeMappings: {})
Related issues/PRs

Nothing found

Suggest a fix

Use Map instead or body custom model with operationId to pascal case as name like this

operationId: login

Model class -> lib/src/model/body/BodyLogin

@wing328
Copy link
Member

wing328 commented May 13, 2022

This should be fixed in the latest master via #12353. Please pull the latest master to give it another try.

@wing328 wing328 closed this as completed May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants