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

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

Open
Nexushunter opened this issue Jul 29, 2023 · 10 comments
Labels
bug Something isn't working

Comments

@Nexushunter
Copy link
Contributor

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 Nexushunter added the bug Something isn't working label Jul 29, 2023
@Nexushunter
Copy link
Contributor Author

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

@gibahjoe
Copy link
Owner

Can you please share a minimal reproducible openapi spec

@gibahjoe
Copy link
Owner

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
Copy link
Contributor Author

Nexushunter commented Jul 29, 2023

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
Copy link
Owner

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
Copy link
Contributor Author

No worries!

Thanks for the tip!

@gibahjoe
Copy link
Owner

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.

@gibahjoe gibahjoe changed the title JsonObject not imported with dio generator Dio generator not correctly processing v3.1.0 spec (converts String to JsonObject) Jul 30, 2023
@Nexushunter
Copy link
Contributor Author

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
Copy link
Contributor Author

Nexushunter commented Aug 1, 2023

👋🏻 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
Copy link
Owner

gibahjoe commented Aug 1, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants