openapi-generator-dart icon indicating copy to clipboard operation
openapi-generator-dart copied to clipboard

A generated object is being assigned to a builder for that object causing a type error

Open indigothm opened this issue 1 year ago • 0 comments

Description of the bug

I have a nested model in my OpenAPI spec:

 Invoice:
      type: object
      properties:
        customer:
          $ref: "#/components/schemas/InvoiceCustomer"
          type: object
        creditMemoRef:
          $ref: "#/components/schemas/InvoiceCreditRef"
          type: object
        changeDate:
          type: string
          format: date-time
        creditMemo:
          type: boolean
        currencyCode:
          type: string
        docInvoiceAmount:
          type: string
        docInvoiceBalance:
          type: string
        invoiceNumber: 
       ...


InvoiceCustomer:
      type: object
      properties:
        customerCountry:
          type: string
        customerTradeAccountId:
          type: string
        customerName:
          type: string
        customerResaleId:
          type: string
        customerState:
          type: string
        customerTerritoryId:
          type: string
      required:
        - customerCountry
        - customerTradeAccountId
        - customerName
        - customerResaleId
        - customerState
        - customerTerritoryId

When I generate a dart client using the generator, the generated code has a type error and doesn't run:

void _deserializeProperties(
    Serializers serializers,
    Object serialized, {
    FullType specifiedType = FullType.unspecified,
    required List<Object?> serializedList,
    required InvoiceBuilder result,
    required List<Object?> unhandled,
  }) {
    for (var i = 0; i < serializedList.length; i += 2) {
      final key = serializedList[i] as String;
      final value = serializedList[i + 1];
      switch (key) {
        case r'customer':
          final valueDes = serializers.deserialize(
            value,
            specifiedType: const FullType(InvoiceCustomer),
          ) as InvoiceCustomer;
          result.customer = valueDes;
          break;
        case r'creditMemoRef':
          final valueDes = serializers.deserialize(
            value,
            specifiedType: const FullType(InvoiceCreditRef),
          ) as InvoiceCreditRef;
          result.creditMemoRef = valueDes;
          break;
        case r'changeDate':
          final valueDes = serializers.deserialize(
            value,
            specifiedType: const FullType(DateTime),
          ) as DateTime;
          result.changeDate = valueDes;
          break;

in the function above result.customer = valueDes is invalid since InvoiceCustomer is being assigned to an InvoiceCustomerBuilder:

A value of type 'InvoiceCustomer' can't be assigned to a variable of type 'InvoiceCustomerBuilder?'.
Try changing the type of the variable or casting the right-hand type to 'InvoiceCustomerBuilder?'.

I have a few objects with this type of errors.

Steps to reproduce

The client is generated with the following config:

@Openapi(
  additionalProperties: DioProperties(
    serializationLibrary: DioSerializationLibrary.jsonSerializable,
    pubName: '...',
    pubAuthor: '...',
    pubDescription: '...',
    pubVersion: '0.0.4',
    pubHomepage: 'https://dev.api.myproject.com.au/swagger/',
  ),
  inputSpec: InputSpec(path: 'lib/src/common/config/api/api.yaml'),
  generatorName: Generator.dio,
  outputDirectory: 'packages/api',
  typeMappings: {
    'SearchSortBy.field': 'SearchSortByFieldEnum',
  },
  reservedWordsMappings: {
    'name': 'itemName',
    'SearchSortByFieldEnum.name': 'itemName',
  },
)

class GenerateApi {}

using fvm dart pub run build_runner build --delete-conflicting-outputs

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.0, on macOS 14.5 23F79 darwin-x64, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.91.1)

Expected behavior

The generated API client runs with no build/type errors.

Logs

No response

Screenshots

No response

Platform

macOS

Library version

5.0.2

Flutter version

3.24

Flutter channel

stable

Additional context

No response

indigothm avatar Aug 29 '24 11:08 indigothm