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] [Dart] Array of array items don't handle required correctly #13460

Closed
5 of 6 tasks
0xNF opened this issue Sep 18, 2022 · 0 comments · Fixed by #13461
Closed
5 of 6 tasks

[BUG] [Dart] Array of array items don't handle required correctly #13460

0xNF opened this issue Sep 18, 2022 · 0 comments · Fixed by #13461

Comments

@0xNF
Copy link
Contributor

0xNF commented Sep 18, 2022

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

Double arrays will generate with invalid defaults in the event of null values encountered during deserialization

This will generate the following invalid deserializer:

class WithNonNullableArray {
  /// Returns a new [WithNonNullableArray] instance.
  WithNonNullableArray({
    this.subarray = const [],
  });

  /* omitted */ 

  List<List<int>> subarray;
  static WithNonNullableArray? fromJson(dynamic value) {
    if (value is Map) {
      final json = value.cast<String, dynamic>();
     /* omitted */
      return WithNonNullableArray(
        subarray: json[r'subarray'] is List
          ? (json[r'subarray'] as List).map(
              (e) => e == null ? null : (e as List).cast<int>()
            ).toList()
          : null,
      );
    }
    return null;
  }
}

Null isn't valid here, and so becomes a syntax error. The proper typing of the defaults for this example should be <int>[]

openapi-generator version

Latest commit as of 9/18/22 (863dbc7)

OpenAPI declaration file content or url
Specfile
openapi: 3.0.3
info:
  version: "1.1"
  title: Dart Uint8list Demo
servers:
  - url: "localhost"
    variables:
      host:
        default: localhost
paths:
  /item:
    get:
      operationId: GetItem
      description: "Should return an Item"
      responses:
        "200":
          description: items
          content:
            application/json:
              schema:
                $ref: "#components/schemas/WithNonNullableArray"
components:
  schemas:
    WithNonNullableArray:
      type: object
      required: [subarray]
      properties:
        subarray:
          type: array
          minItems: 12
          maxItems: 12
          items:
            $ref: "#/components/schemas/SubArray"
    WithNullableArray:
      type: object
      required: [subarray]
      properties:
        months:
          type: array
          nullable: true
          items:
            $ref: "#/components/schemas/SubArray"
    SubArray:
      type: array
      items:
        type: integer
        format: int32
Suggest a fix

Changing the native_class.mustache file to include

(e) => e == null ? {{#required}}{{#isNullable}}null{{/isNullable}}{{^isNullable}}const <{{items.items.dataType}}>[]{{/isNullable}}{{/required}}{{^required}}null{{/required}}

Changes the output to be more appropriate:

      return WithNonNullableArray(
        subarray: json[r'subarray'] is List
          ? (json[r'subarray'] as List).map(
              (e) => e == null ? const <int>[] : (e as List).cast<int>()
            ).toList()
          :  const <int>[],
      );
wing328 pushed a commit that referenced this issue Dec 27, 2022
…13461)

* [Dart] Added non-invalid defaults for non nullable array of arrays (#13460)

* [Dart] Update samples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant