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

[BUG] Generates List<<List<SomeGet$Response>> instead of List<SomeGet$Response> #711

Open
anasJasim opened this issue Jan 11, 2024 · 3 comments · May be fixed by #792
Open

[BUG] Generates List<<List<SomeGet$Response>> instead of List<SomeGet$Response> #711

anasJasim opened this issue Jan 11, 2024 · 3 comments · May be fixed by #792
Assignees
Labels
bug Something isn't working Triage needed

Comments

@anasJasim
Copy link

anasJasim commented Jan 11, 2024

Describe the bug
The code generation process for the OpenAPI Swagger Dart library is producing incorrect output. Instead of generating a response type of List<ItemsGet$Response>, the code generator is generating List<List<ItemsGet$Response>>.

Generated output
File: openapi.swagger.dart

abstract class Openapi extends ChopperService {
    @Get(path: '/items')
    Future<chopper.Response<List<ItemsGet$Response>>> _itemsGet(
        {@Query('tenantUnitId') required int? tenantUnitId});
}
// ...
typedef ItemsGet$Response = List<ItemsGet$Response$Item>;

Expected output
File: openapi.swagger.dart

abstract class Openapi extends ChopperService {
    @Get(path: '/items')
    Future<chopper.Response<ItemsGet$Response>> _itemsGet(
        {@Query('tenantUnitId') required int? tenantUnitId});
}
// ...
typedef ItemsGet$Response = List<ItemsGet$Response$Item>;

Swagger specification link
Link to swagger/OpenApi documentation

build.yaml

targets:
  $default:
    builders:
      swagger_dart_code_generator:
        options:
          input_folder: "lib/openapi/swaggers/"
          output_folder: "lib/openapi/generated_code/"
          input_urls:
            - url: "http://127.0.0.1:3001/openapi.json"

Library version used:
chopper: ^6.1.1
json_annotation: ^4.8.0

build_runner: ^2.3.3
chopper_generator: ^6.0.0
json_serializable: ^6.6.1
swagger_dart_code_generator: ^2.10.4

@anasJasim anasJasim added bug Something isn't working Triage needed labels Jan 11, 2024
@dJani97
Copy link

dJani97 commented Feb 20, 2025

I have the same issue with a similar schema, mine also contains the same structure:

"schema": {
  "type": "array",
  "items": {
    "type": "object",

@anasJasim did you find any solution?

I think one possible way of fixing it is to rewrite your schema like so:

"schema": {
  "$ref": "#/components/schemas/SomeSchema"
}

These types of schemas work well for me with the generator. But this is of course not possible on a large scale, and an unnecessary manual step.

My framework & lib version:
Flutter: 3.29.0
swagger_dart_code_generator: ^3.0.1

@anasJasim
Copy link
Author

Unfortunately @dJani97 I haven't found a clean solution yet. But business must go on haha, so here is my hacky solution:

# Items
- url: "/items"
  method: post
  overridden_value: "dynamic"
- url: "/items"
  method: get
  overridden_value: "dynamic"
# ...

Add above to build.yaml. When you do so you also encounter another issue and for that I have created post-swagger-bug-fix.sh

#!/bin/bash

file="./lib/src/utils/openapi/generated_code/openapi.swagger.dart"

search_string="generatedMapping.putIfAbsent(\$Dynamic, () => \$Dynamic.fromJsonFactory);"

grep -v "$search_string" "$file" > "$file.tmp" && mv "$file.tmp" "$file"

echo "Lines containing '$search_string' have been removed in '$file'."

Your solution might be cleaner, but it wouldn't work if you use an automated OpenAPI generator in the backend.

@flowhorn
Copy link

flowhorn commented Mar 2, 2025

Hey, I made a PR which should fix this issue, removed one .asList() in the generation code:

#792

You can try it out using:

  swagger_dart_code_generator:
    git: https://github.com/flowhorn/swagger-dart-code-generator.git

Apparently this doesn't fully work for me, as there is still some issue then with the json_decoder which requires some adjustments.

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