Skip to content

[DART-DIO-NEXT] error using query parameter with BuiltList of EnumClass #11250

@ktnishide

Description

@ktnishide

@kuhnroyal @agilob

Hi everyone!

Is there any workaround for handling an array of enum on query parameters like below?
image

I am getting the following error:

[log] type 'String' is not a subtype of type 'SearchType' in type cast
[log] #0      _CastListBase.[] (dart:_internal/cast.dart:99:46)
#1      ListMixin.elementAt (dart:collection/list.dart:78:33)
#2      ListIterator.moveNext (dart:_internal/iterable.dart:342:26)
#3      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:188:27)
#4      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#5      new List.of (dart:core-patch/array_patch.dart:51:28)
#6      encodeCollectionQueryParameter

Generated code causing the error:
api_util.dart

ListParam<T> encodeCollectionQueryParameter<T>(
  Serializers serializers,
  dynamic value,
  FullType type, {
  ListFormat format = ListFormat.multi,
}) {
  final serialized = serializers.serialize(
    value as Object,
    specifiedType: type,
  );
  if (value is BuiltList<T> || value is BuiltSet<T>) {
    return ListParam(List.of((serialized as Iterable<Object?>).cast()), format);
  }
  throw ArgumentError('Invalid value passed to encodeCollectionQueryParameter');
}

OpenApi Spec:

  /feed:
    get:
      tags:
        - FeedApi
      parameters:
        - name: state
          in: query
          schema:
            type: string
            format: string
        - name: top
          in: query
          schema:
            type: integer
            format: int32
        - name: skip
          in: query
          schema:
            type: integer
            format: int32
        - name: contentType
          in: query
          schema:
            type: array
            items:
              $ref: '#/components/schemas/SearchType'
        - name: categories
          in: query
          schema:
            type: array
            items:
              $ref: '#/components/schemas/AppArticleCategory'
      responses:
        '200':
          description: Ok
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/FeedItemViewModel'
      x-operation-name: get
...
components:
  schemas:
    AppArticleCategory:
      enum:
        - StudentLife
        - CareerAndMoney
        - HealthAndRelationship
        - MoneyAndFinance
      type: string
    SearchType:
      enum:
        - All
        - Article
        - PartTimeJob
        - Career
        - Course
        - Offer
        - Competition
        - ForumThread
        - Survey
      type: string

Originally posted by @ktnishide in #8536 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions