swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.97k stars 6.04k forks source link

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

Open thomas-watchenterprise opened 1 year ago

thomas-watchenterprise commented 1 year ago

Q&A (please complete the following information)

Content & configuration

-Load the petstore API 3.0

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

char0n commented 1 year ago

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.

vandadnp commented 1 year ago

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!

oomichi commented 1 year ago

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

/cc @oomichi

oomichi commented 1 year ago

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

St1chaKooky commented 1 month ago

Hello everyone, I encountered a problem with swagger version 3.0.0

targets: $default: sources:

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?