OpenAPITools / OpenAPITools/openapi-generator

[BUG][dart][dart-dio] Error generating Dart client-side code from OpenAPI API JSON

Open
#16,243 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

I am trying to generate the client-side code of an OpenAPI API using the openapi-generator-cli with the Dart-Dio generator. However, I am encountering errors during the generation process. The API JSON provided is just a small part of the whole API, and we have already successfully generated the API client code using the typescript-angular generator for Angular without any issues.

openapi-generator version

Should be 6.6.0

Contents of the openapitools.json file:

{
  "$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "6.6.0"
  }
}
OpenAPI declaration file content or url

Link to OpenAPI declaration file

Generation Details

Script I am using for generation:

cd packages/apis

echo "Removing old files..."
rm -rf client_api

echo "Generating client api..."
openapi-generator-cli generate -i "../../scripts/api_snippet.json" -g dart-dio -o client_api

cd client_api

echo "Adding dependencies..."
flutter pub add build_runner
flutter pub add analyzer:0.39.14

echo "Cleaning up..."
flutter clean
flutter pub cache repair
flutter pub run build_runner clean

echo "Getting dependencies..."
flutter pub get

echo "Running build_runner..."
dart pub run build_runner build --delete-conflicting-outputs

I've also tried using the dart openapi generator, but this also did not work out. I think I would like to have it with dart-dio, since this seems to be the most liked option by the flutter community.

Logs after running command: Logs

Contents of the generated pubspec.yaml file: pubspect.yaml

Expected Outcome: I expect the Dart client-side code to be successfully generated without any syntax errors.

Actual Outcome: The generation process fails with syntax errors in the token_info.dart file.

Contents of the token_info.dart file: token_info.dart

Error when hovering over part 'token_info.g.dart:

Target of URI hasn't been generated: 'package:openapi/src/model/token_info.g.dart'.
Try running the generator that will generate the file referenced by the URI. dart([uri_has_not_been_generated](https://dart.dev/diagnostics/uri_has_not_been_generated))

TokenInfoBuilder is also underlined in red everywhere and shows the following error when hovering over:

Undefined class 'TokenInfoBuilder'.
Try changing the name to the name of an existing class, or creating a class with the name 'TokenInfoBuilder'. dart([undefined_class](https://dart.dev/diagnostics/undefined_class))
Steps to reproduce

Since this API client code generation is created as a package, I think there are no steps to reproduce this other than just using the shell script. You may have to install the openapi-generator-cli and flutter.

Related issues/PRs

This could be a similar issue

Suggest a fix

As a disclaimer, I must admit that I am more of a beginner when it comes to Flutter. If you have any questions or suggestions regarding the problem and the provided logs, please feel free to ask. I will do my best to address any queries and work towards finding a solution. Thank you for taking the time to review my problem and for any assistance you can provide!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the dart-dio generation command from the issue with scripts/api_snippet.json and openapitools.json. Inspect the generated token_info.dart, the linked logs, and pubspec.yaml to identify why token_info.g.dart and TokenInfoBuilder are missing. Done means the sample client generates and builds without those Dart syntax and unresolved-symbol errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.