epam-cross-platform-lab / swagger-dart-code-generator

Swagger/OpenAPI code generator based on Chopper and JsonAnnotation for Flutter
Apache License 2.0
261 stars 119 forks source link

[BUG] OpenAPI 3.1 nullable types not supported #741

Open juliansteenbakker opened 6 months ago

juliansteenbakker commented 6 months ago

I'm having some issues using the generated json to generate API endpoint for Flutter using the this package. It seems to fail on the notation for nullable types.

It seems as if the following notation for a nullable value (expires_at) which is valid in the openAPI 3.1 editor, is not being generated correctly.

      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                role:
                  type: string
                expires_at:
                  type:
                    - string
                    - 'null'

when i change the type to type: string, everything works as expected error:

type 'List<dynamic>' is not a subtype of type 'String?' in type cast
package:swagger_dart_code_generator/src/swagger_models/responses/swagger_schema.g.dart 11:26  _$SwaggerSchemaFromJson
package:swagger_dart_code_generator/src/swagger_models/responses/swagger_schema.dart 118:7    new SwaggerSchema.fromJson
package:swagger_dart_code_generator/src/swagger_models/responses/swagger_schema.g.dart 16:43  _$SwaggerSchemaFromJson.<fn>

Swagger specification link 3.1.0

Library version used: 2.15.2

CarlMichaelFabe commented 5 months ago

I tried the fixes and it mostly works, except on nullable lists.

numbermess commented 4 months ago

I updated my project's pubspec.yaml entry for swagger_dart_code_generator to

#  swagger_dart_code_generator: ^2.15.2
  swagger_dart_code_generator:
    git:
      url: https://github.com/juliansteenbakker/swagger-dart-code-generator.git
      ref: openapi-3.1-fix

And I was able to build the client library without error for resources that had a list of types that included null, like:

...
          "account": {
            "type": [
              "string",
              "null"
            ],
            "format": "iri-reference",
            "example": "https://example.com/"
          },
...

This got rid of the "type 'List' is not a subtype of type 'String?' in type cast" error for me. Thanks!