swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

Client code in Dart doesn't compile with latest Flutter SDK

Open thomas-watchenterprise opened this issue 2 years ago • 5 comments

Q&A (please complete the following information)

  • OS: Windows 10
  • Browser: Firefox
  • Version: 109.0.1
  • Method of installation: [e.g. npm, dist assets] webapp
  • Swagger-Editor version: [e.g. 3.10.0]
  • Swagger/OpenAPI version: [OpenAPI 3.0]

Content & configuration

-Load the petstore API 3.0

  • generate Dart Client code and open it on a system with the latest Flutter /Dart SDK installed

Describe the bug you're encountering

It clearly shows that the generated code isn't compatible with the latest Dart compiler versions. Especially it hasn't be adapted to non nullability of the Dart language, so the code has lots and lots of warnings and errors:

grafik

Expected behavior

production of valid Dart code

Additional context or thoughts

depending on how your code generator is implemented I maybe able to help you fix the code I really like the idea of using SwaggerHub in our company but without valid Dart code it doesn't make sense

thomas-watchenterprise avatar Feb 07 '23 16:02 thomas-watchenterprise

Hi @thomas-watchenterprise,

Thanks for the report. It looks like this issue is related to https://github.com/swagger-api/swagger-codegen which SwaggerEditor just uses under the hood. I'm transferring the issue under the codegen.

char0n avatar Feb 09 '23 16:02 char0n

Hello everyone,

Thank you for Swagger-Codegen, we are using it successfully for our iOS and Android native apps and really love it. I appreciate all the hard work that's gone into creating it 👍

We are also facing issues with the Flutter code-generation where the code generator is =dart

On macOS, I use this command to generate our client

swagger-codegen generate \
    -i http://localhost:5555/swagger.json \
    -l dart \
    -o api \
    -c swagger/config.json

And the swagger/config.json file is as follows:

{
    "pubName": "api",
    "pubVersion": "0.0.1",
    "pubDescription": "API for the application",
    "useEnumExtension": true,
    "browserClient": false
}

I'll list the issues here:


Issue 1 The generated pubspec.yaml for the library does not contain the environment.sdk value, as shown here:

name: api
version: 0.0.1
description: API for the application
dependencies:
  http: '>=0.11.1 <0.13.0'
dev_dependencies:
  test: ^1.3.0

So right after doing a dart pub get, we receive the following error:

dart pub get
Resolving dependencies...
pubspec.yaml has no lower-bound SDK constraint.
You should edit pubspec.yaml to contain an SDK constraint:

environment:
  sdk: '^2.12.0'

See https://dart.dev/go/sdk-constraint
exit code 65

So I went ahead and added that environment.sdk value as suggested by the above (but I changed it to 2.0.0) command and then I can successfully do a flutter pub get or dart pub get! So that's the solution to this issue.


Issue 2 The generated API doesn't support Dart's sound-null-safety!

So when I try to compile and run the Flutter app now for iOS, I get the following error:

error: Error: Cannot run with sound null safety, because the following dependencies
don't support null safety:

 - package:http
 - package:http_parser

For solutions, see https://dart.dev/go/unsound-null-safety

What I did was to go in Visual Studio Code's workspace settings and add this configuration:

{
    "dart.flutterRunAdditionalArgs": [
        "--no-sound-null-safety",
    ]
}

So the above fixed issue 2


But this makes the generated Dart client pretty much obsolete from the get-go because it doesn't support sound-null-safety!

vandadnp avatar Apr 20 '23 04:04 vandadnp

I also am facing this issue and the other people also are according to #11294

/cc @oomichi

oomichi avatar Jul 24 '23 05:07 oomichi

I tried to use openapi generator v6.6.0 instead, that works well for me without this issue.

oomichi avatar Jul 28 '23 07:07 oomichi

Hello everyone, I encountered a problem with swagger version 3.0.0

targets: $default: sources: - swaggers/** - lib/** builders: chopper_generator: options: header: "//Generated code" swagger_dart_code_generator: options: input_folder: "lib/" output_folder: "lib/swagger_generated_code/" separate_models: true ignore_headers: true

This is what my yaml looks like. Снимок экрана 2024-08-19 145932

This is what the terminal outputs after dart build run The error states that the type null cannot be String, but I specified my input_folder with a value. Why is this so?

wov1a avatar Aug 19 '24 12:08 wov1a