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-dio] optimize parameterToString to convert DateTime correctly #8271

Closed
ahmednfwela opened this issue Dec 24, 2020 · 18 comments · Fixed by #9635
Closed

[BUG][dart-dio] optimize parameterToString to convert DateTime correctly #8271

ahmednfwela opened this issue Dec 24, 2020 · 18 comments · Fixed by #9635

Comments

@ahmednfwela
Copy link
Contributor

Description

Currently DateTime is being converted to string using normal .ToString()
Ref:

String parameterToString(Serializers serializers, dynamic value) {
if (value == null) {
return '';
} else if (value is String || value is num) {
return value.toString();
} else {
return json.encode(serializers.serialize(value));
}
}

instead, toIso8601String should be used

openapi-generator version

V5.0.0

Suggested fix:
  if (value == null) {
    return '';
  } else if (value is String || value is num) {
    return value.toString();
  } else if (value is DateTime) {
    return value.toIso8601String();
  } else {
    return json.encode(serializers.serialize(value));
  }
@ahmednfwela ahmednfwela changed the title [BUG][DART-DIO] optimize parameterToString to convert DateTime correctly [BUG][dart-dio] optimize parameterToString to convert DateTime correctly Dec 24, 2020
@kuhnroyal
Copy link
Contributor

If it is a DateTime it should use the configured serializer that is also used for JSOn content. Is this not happening?

@ahmednfwela
Copy link
Contributor Author

no, even bool isn't serialized correctly
it outputs a json object rather than a json value

@kuhnroyal
Copy link
Contributor

Yea we should not serialize primitives with a serializer.

@kuhnroyal
Copy link
Contributor

Can you please review and possibly test the PR

@kuhnroyal
Copy link
Contributor

If no specific serializer is used, built_value will add a discriminator field and make it a JSON object.
The check for bool was missing.

@ahmednfwela
Copy link
Contributor Author

I think the issue here is that built_value should have handled primitives better than this, serialization shouldn't be so over-complicated.
I think we should file an issue at their side to handle this while we temporarily fix it.

@kuhnroyal
Copy link
Contributor

parameterToString is only used for form data, we always need a normal String representation. That is not really what built_value is meant to be used for. Maybe the problem is triggered by the json.encode call.

In any case, for DateTime it should have used the registered Iso8601DateTimeSerializer.

@kuhnroyal
Copy link
Contributor

I have update the PR and added tests, can you check if that solves your issues?

@ahmednfwela
Copy link
Contributor Author

I have been using my own templates, and I believe they solve most of the issues that I came across

https://github.com/Bdaya-Dev/Custom-Openapi-Templates

They have been tested on multiple specifications too

@kuhnroyal
Copy link
Contributor

Ok so I have not thought about collections. Looking at the Dio FormData implementation makes a couple things clearer: https://github.com/flutterchina/dio/blob/master/dio/lib/src/utils.dart#L34

It seems your list.join(',') is some custom handling that FormData doesn't support by default?

@ahmednfwela
Copy link
Contributor Author

the problem is the way backend handles requests, some want it to be path[index]=value, others path=value0,value1,value2
this is highly opinionated, so it depends on what I am currently using

@ahmednfwela
Copy link
Contributor Author

@kuhnroyal
Copy link
Contributor

I think #8372 could solve most of it. We can not currently support custom collection formats. I added tests to ensure collections and date time is correctly encoded in the Dio FormData.
Dio encodes lists with braces but without index path[]=value1&path[]=value2. If you need different encoding then you still need to adapt the templates according to your needs.

@kuhnroyal
Copy link
Contributor

After adding more tests, I noticed that this is also a question of multipart/form-data vs application/x-www-form-urlencoded. We are using the same method to prepare the parameters but Dio encodes them differently.
What are your use cases? multipart/form-data or application/x-www-form-urlencoded?

@ahmednfwela
Copy link
Contributor Author

most of them are multipart/form-data

@kuhnroyal
Copy link
Contributor

Both should now work with collections and path[]=value1&path[]=value2 format.
We can not configure the collection format for application/x-www-form-urlencoded until cfug/dio#808 is released.

@kuhnroyal
Copy link
Contributor

A way to handle collection formats has been added to dio 4.0.0. If you need different formats in one route you might want to comment on cfug/dio#799 and follow #6681

Otherwise I think this issue can be closed.

@kuhnroyal
Copy link
Contributor

The application/x-www-form-urlencoded part should now be handled correctly by #9635

kuhnroyal added a commit to kuhnroyal/openapi-generator that referenced this issue Jun 5, 2021
* add support for collectionFormat in query parameters and www-url-encoded content
* add tests
* remove empty query parameter maps from generated code
* closes OpenAPITools#6681
* closes OpenAPITools#9522
* closes OpenAPITools#8271 (the last remaining parts)
wing328 pushed a commit that referenced this issue Jun 7, 2021
…9635)

* [dart][dart-dio] Add support for query collection parameter format

* add support for collectionFormat in query parameters and www-url-encoded content
* add tests
* remove empty query parameter maps from generated code
* closes #6681
* closes #9522
* closes #8271 (the last remaining parts)

* Fix formatting
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.

2 participants