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

serializers.deserialize fails for Nullable Types such as String?

Open flodaniel opened this issue 4 years ago • 2 comments

When trying to deserialize an generated Object with the following code, I get an error, that Null cannot be cast to String. serializers.fromJson<User?>(User.serializer, json)

The error: Unhandled error Unhandled error Deserializing '[id, 10, firstName, a5, lastName, a5, email, [email protected], password, null, cr...' to 'User' failed due to: type 'Null' is not a subtype of type 'String' in type cast occurred in Instance of 'AuthenticationBloc'.

This seems to be related to this bug report in the package built_value: https://github.com/google/built_value.dart/issues/1035 The issue seems to be fixed according to the comments there, but the generated code for the deserialize part of my user object still looks like this:

case r'providerId':
                    result.providerId = serializers.deserialize(value,
                        specifiedType: const FullType(String)) as String;

even though it should be looking like this, as providerId is a nullable string:

case r'providerId':
                    result.providerId = serializers.deserialize(value,
                        specifiedType: const FullType(String)) as String?;

Changing the generated code fixes the issue.

I am using the following packages, and the Generator.dioNext.

  openapi_generator: ^3.2.0+2
  openapi_generator_annotations: ^3.2.0

This makes it even more confusing, as in the generated pubspec.lock I am seeing that the code is using the latest built_value version (which is 8.1.1

# pubspec.lock snippet from api/lib generated code. 
  built_value:
    dependency: "direct main"
    description:
      name: built_value
      url: "https://pub.dartlang.org"
    source: hosted
    version: "8.1.1"

Anything I am missing, or a genuine bug?

flodaniel avatar Jul 22 '21 20:07 flodaniel

@flodaniel , hi, could you find a proper way to solve this?

radikris avatar Nov 09 '21 16:11 radikris

Hey, I moved away from the generator and now we use chopper only.

flodaniel avatar Nov 09 '21 17:11 flodaniel