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
[dart-dio] Add toString in Enum on json_serializer #15387
[dart-dio] Add toString in Enum on json_serializer #15387
Conversation
@jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12) @ahmednfwela (2021/08) |
modules/openapi-generator/src/main/resources/dart/libraries/dio/pubspec.mustache
Outdated
Show resolved
Hide resolved
What are the general thoughts on bumping Dart to 2.17? |
I think it would be fine really |
can we limit this version bump for json_serializer only? |
I'm sorry. |
I modified it. |
@@ -11,7 +11,14 @@ enum {{{classname}}} { | |||
/// {{{.}}} | |||
{{/description}} | |||
@JsonValue({{#isString}}r{{/isString}}{{{value}}}) | |||
{{{name}}}, | |||
{{{name}}}({{^isString}}'{{/isString}}{{#isString}}r{{/isString}}{{{value}}}{{^isString}}'{{/isString}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are numbers converted to strings ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the generator does not convert it to a string, the generated code will result in a compile error.
I wanted to change the name
type to each type, but it was difficult for me.
Do you have any ideas?
- before
enum OuterEnumInteger {
@JsonValue(0)
number0(0), // compile error
@JsonValue(1)
number1(1), // compile error
@JsonValue(2)
number2(2), // compile error
@JsonValue(11184809)
unknownDefaultOpenApi(11184809); // compile error
const OuterEnumInteger(this.name);
final String name;
@override
String toString() => name;
}
- after
enum OuterEnumInteger {
@JsonValue(0)
number0('0'),
@JsonValue(1)
number1('1'),
@JsonValue(2)
number2('2'),
@JsonValue(11184809)
unknownDefaultOpenApi('11184809');
const OuterEnumInteger(this.name);
final String name;
@override
String toString() => name;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of string name ,use int value or string value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change correct?
import 'package:json_annotation/json_annotation.dart';
{{#description}}/// {{{description}}}{{/description}}
{{#isDeprecated}}
@Deprecated('{{{classname}}} has been deprecated')
{{/isDeprecated}}
enum {{{classname}}} {
{{#allowableValues}}
{{#enumVars}}
{{#description}}
/// {{{.}}}
{{/description}}
@JsonValue({{#isString}}r{{/isString}}{{{value}}})
{{{name}}}({{#isString}}r{{/isString}}{{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}
{{/allowableValues}}
const {{{classname}}}(this.value);
final {{#isInteger}}int{{/isInteger}}{{^isInteger}}String{{/isInteger}} value;
@override
String toString() => value{{^isString}}.toString(){{/isString}};
}
This part might be better changed to Integer.
String toString() => value{{^isInteger}}.toString(){{/isInteger}};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does the spec allow decimal enums?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking we do
final Object? value;
instead of trying to figure out the type, but @kuhnroyal can give his input here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does the spec allow decimal enums?
I could not find this description.
https://swagger.io/docs/specification/data-models/enums/
By the way, I tried this code.
The generator converts number
to string
.
- enum.mustache
import 'package:json_annotation/json_annotation.dart';
{{#description}}/// {{{description}}}{{/description}}
{{#isDeprecated}}
@Deprecated('{{{classname}}} has been deprecated')
{{/isDeprecated}}
enum {{{classname}}} {
{{#allowableValues}}
{{#enumVars}}
{{#description}}
/// {{{.}}}
{{/description}}
@JsonValue({{#isString}}r{{/isString}}{{{value}}})
{{{name}}}({{#isString}}r{{/isString}}{{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}
{{/allowableValues}}
const {{{classname}}}(this.value);
final {{#isInteger}}int{{/isInteger}}{{^isInteger}}String{{/isInteger}} value;
@override
String toString() => value{{^isString}}.toString(){{/isString}};
}
Integer
- petstore-with-fake-endpoints-models-for-testing.yaml
OuterEnumInteger:
type: integer
enum:
- 0
- 1
- 2
- generated code
enum OuterEnumInteger {
@JsonValue(0)
number0(0),
@JsonValue(1)
number1(1),
@JsonValue(2)
number2(2),
@JsonValue(11184809)
unknownDefaultOpenApi(11184809);
const OuterEnumInteger(this.value);
final int value;
@override
String toString() => value.toString();
}
Double(number)
- petstore-with-fake-endpoints-models-for-testing.yaml
OuterEnumInteger:
type: number
enum:
- 0.5
- 1.5
- 2.5
example: 2.5
- generated code
enum OuterEnumInteger {
@JsonValue('0.5')
n0period5('0.5'),
@JsonValue('1.5')
n1period5('1.5'),
@JsonValue('2.5')
n2period5('2.5'),
@JsonValue('11184809')
unknownDefaultOpenApi('11184809');
const OuterEnumInteger(this.value);
final String value;
@override
String toString() => value.toString();
}
Others
cf. https://swagger.io/docs/specification/data-models/data-types/
- boolean
I don't think we need to consider this type.
enum OuterEnumInteger {
@JsonValue('true')
true_('true'),
@JsonValue('false')
false_('false'),
@JsonValue('11184809')
unknownDefaultOpenApi('11184809');
const OuterEnumInteger(this.value);
final String value;
@override
String toString() => value.toString();
}
-
array
not works -
object
not works
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not go with something like this
@JsonEnum(valueField: 'value')
enum SomeEnum {
trueBoolValue(true),
falseBoolValue(false),
n8period7(8.7),
n3(3),
stringValue('str');
const SomeEnum(this.value);
final Object value;
@override
String toString() => value.toString();
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't convert number
and boolean
.
After conversion, {{{value}}} in mustache becomes a string in a dart file.
Do you know the solution?
- integer
yaml: 100
{{{value}}}
converts to 100
- number
yaml: 0.5
{{{value}}}
converts to '0.5'
- boolean
yaml: true
{{{value}}}
converts to 'true'
...enerator/src/main/resources/dart/libraries/dio/serialization/json_serializable/enum.mustache
Outdated
Show resolved
Hide resolved
Can this pr get some attention please we're also dependent on this change. |
Hi seniors! |
Hi @ahmednfwela |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if others are ok with values being converted to string I don't mind
This doesn't work for enums that are defined inline as part of another object. These will generate in the same dart file as the object that references them and they will not get the value/toString generated |
Fix #15382
I implemented toString in Enum on dart-dio, which can be used from Dart 2.17.
PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.For Windows users, please run the script in Git BASH.
master
(6.3.0) (minor release - breaking changes with fallbacks),7.0.x
(breaking changes without fallbacks)