gibahjoe / openapi-generator-dart

Openapi Generator for Dart/Flutter
BSD 3-Clause "New" or "Revised" License
119 stars 32 forks source link

Dio generator not correctly processing v3.1.0 spec (converts String to JsonObject) #94

Open Nexushunter opened 1 year ago

Nexushunter commented 1 year ago

Description of the bug

When using this config:

const _apiLink = 'https://api.tagmine.ca/openapi.yaml';

@Openapi(
  additionalProperties: DioProperties(
    pubName: 'tagmine_api',
    pubDescription: 'OAS generated dart api for tagmine.ca',
    useEnumExtension: true,
    sortParamsByRequiredFlag: true,
    sortModelPropertiesByRequiredFlag: true,
    nullableFields: false
  ),
  inputSpecFile: _apiLink,
  generatorName: Generator.dio,
  outputDirectory: 'clients/dart',
  overwriteExistingFiles: false,
  alwaysRun: true,
  fetchDependencies: true,
  // Try to add missing import???
  importMappings: {
    'JsonObject': 'package:built_value/json_object.dart',
  },
)
class TagmineClientConfig extends OpenapiGeneratorConfig {}

The resulting library produces incorrect artifacts. All of the src/api/*_api.dart files are missing the JsonObject import which is imported from package:built_value/json_object.dart

Steps to reproduce

Run dart run build_runner build --delete-conflicting-outputs with this in the build.yml

builders:
  openapi_generator:
    target: ":openapi_generator"
    import: "package:openapi_generator/src/builder.dart"
    builder_factories: [ "openApiClientSdk" ]
    build_extensions: { ".dart": [ ".g.part" ] }
    auto_apply: dependents
    build_to: cache
    applies_builders: [ "source_gen|combining_builder" ]

targets:
  $default:
    builders:
      openapi_generator:openapi_generator_annotations:
        generate_for:
          - lib/client_generator_config.dart

and the contents of the config within lib/client_generator_config.dart

Expected behavior

No missing imports in generated code.

Logs

No response

Screenshots

No response

Platform

Linux

Library version

4.11.0

Flutter version

3.0.5 - Dart

Flutter channel

stable

Additional context

This is done purely in dart project.
Nexushunter commented 1 year ago

I'm up to help out, but I have no idea where to start 😄

gibahjoe commented 1 year ago

Can you please share a minimal reproducible openapi spec

gibahjoe commented 1 year ago

Also when you run the build runner, the generated command is shown in the logs please share. Looks like generate -i ../openapi-spec.yaml -g dart-dio ...

Nexushunter commented 1 year ago

The oas spec included above is public but here is a minimal example:

I pulled the spec down and updated my generator config: inputSpecFile: './openapi.yaml'

As an aside: Is there a way to prevent the generation of tests?

openapi: 3.1.0
info:
  title: Site
  description: Test
  version: 0.0.1
servers:
- url: "http://localhost:8080/dev"
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
tags:
  - name: metadata
paths:
  /url_metadata:
    get:
      tags:
        - metadata
      security:
        - bearerAuth: []
      summary: Get title/thumbnail from URL
      parameters:
        - name: url
          in: query
          description: URL of link
          required: true
          schema:
            type:
              - string
      responses:
        '200':
          description: Successful operation

Logs:

[INFO] openapi_generator on lib/client_generator_config.dart:OpenapiGenerator :: [generate -i ./openapi.yaml -g dart-dio -o clients/dart --import-mappings=JsonObject=package:built_value/json_object.dart --additional-properties=pubName=tagmine_api,pubDescription=OAS generated dart api for tagmine.ca,useEnumExtension=true,sortPara[WARNING] openapi_generator on lib/client_generator_config.dart: :: Install exited with code 0 [WARNING] No actions completed for 15.0s, waiting on: - openapi_generator on lib/client_generator_config.dart [SEVERE] openapi_generator on lib/client_generator_config.dart: [INFO] Running build completed, took 23.9s
gibahjoe commented 1 year ago

Ah. Sorry, I didn't see you included the spec.

Any files you don't want to be generated should be added to .openapi-generator-ignore. You can find this file in the root of your generated api. So just add the line test/* to it so it doesn't generate tests.

Nexushunter commented 1 year ago

No worries!

Thanks for the tip!

gibahjoe commented 1 year ago

So, I just looked into this issue you are having with the JsonObject. First, import mappings are applied in the models not the api classes (the models are imported into the apis) that's why the mapping wasn't working.

The real problem is the generator is having trouble parsing the 3.1.0 spec that you have.

A way to work around the issue is download the spec from the url and modify the first line from openapi: 3.1.0 to openapi: 3.0.3. This should resolve this JsonObject issue.

Now, the spec still has some other errors like defining a type as int64 instead of integer. You can use typeMappings to fix that.

Let me know if this is enough for you.

Nexushunter commented 1 year ago

I noticed the type issues and am addressing those! Thanks for the quick turn around time. If you'd like some help LMK I'd love to assist

Nexushunter commented 1 year ago

👋🏻 I'm also noticing that the return values are Future<Response<void> which is causing type erasure. But the response definition looks something like: (Regenerated with v3.0.3)

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

    return _response;
gibahjoe commented 1 year ago

This an issue with the spec as it does not define response types only response codes