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

[dart-dio] Add toString in Enum on json_serializer #15387

Merged
merged 5 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: {{pubDescription}}
homepage: {{pubHomepage}}

environment:
sdk: '>=2.15.0 <3.0.0'
sdk: '>={{#useJsonSerializable}}2.17.0{{/useJsonSerializable}}{{^useJsonSerializable}}2.15.0{{/useJsonSerializable}} <3.0.0'

dependencies:
dio: '^5.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Copy link
Contributor

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 ?

Copy link
Contributor Author

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;
}

Copy link
Contributor

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

Copy link
Contributor Author

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}};

Copy link
Contributor

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?

Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

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();
}

Copy link
Contributor Author

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'

{{/enumVars}}
{{/allowableValues}}

const {{{classname}}}(this.value);

final String value;

@override
String toString() => value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import 'package:json_annotation/json_annotation.dart';

enum ModelEnumClass {
@JsonValue(r'_abc')
abc,
abc(r'_abc'),
@JsonValue(r'-efg')
efg,
efg(r'-efg'),
@JsonValue(r'(xyz)')
leftParenthesisXyzRightParenthesis,
leftParenthesisXyzRightParenthesis(r'(xyz)'),
@JsonValue(r'unknown_default_open_api')
unknownDefaultOpenApi,
unknownDefaultOpenApi(r'unknown_default_open_api');

const ModelEnumClass(this.value);

final String value;

@override
String toString() => value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import 'package:json_annotation/json_annotation.dart';

enum OuterEnum {
@JsonValue(r'placed')
placed,
placed(r'placed'),
@JsonValue(r'approved')
approved,
approved(r'approved'),
@JsonValue(r'delivered')
delivered,
delivered(r'delivered'),
@JsonValue(r'unknown_default_open_api')
unknownDefaultOpenApi,
unknownDefaultOpenApi(r'unknown_default_open_api');

const OuterEnum(this.value);

final String value;

@override
String toString() => value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import 'package:json_annotation/json_annotation.dart';

enum OuterEnumDefaultValue {
@JsonValue(r'placed')
placed,
placed(r'placed'),
@JsonValue(r'approved')
approved,
approved(r'approved'),
@JsonValue(r'delivered')
delivered,
delivered(r'delivered'),
@JsonValue(r'unknown_default_open_api')
unknownDefaultOpenApi,
unknownDefaultOpenApi(r'unknown_default_open_api');

const OuterEnumDefaultValue(this.value);

final String value;

@override
String toString() => value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import 'package:json_annotation/json_annotation.dart';

enum OuterEnumInteger {
@JsonValue(0)
number0,
number0('0'),
@JsonValue(1)
number1,
number1('1'),
@JsonValue(2)
number2,
number2('2'),
@JsonValue(11184809)
unknownDefaultOpenApi,
unknownDefaultOpenApi('11184809');

const OuterEnumInteger(this.value);

final String value;

@override
String toString() => value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import 'package:json_annotation/json_annotation.dart';

enum OuterEnumIntegerDefaultValue {
@JsonValue(0)
number0,
number0('0'),
@JsonValue(1)
number1,
number1('1'),
@JsonValue(2)
number2,
number2('2'),
@JsonValue(11184809)
unknownDefaultOpenApi,
unknownDefaultOpenApi('11184809');

const OuterEnumIntegerDefaultValue(this.value);

final String value;

@override
String toString() => value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ import 'package:json_annotation/json_annotation.dart';

enum SingleRefType {
@JsonValue(r'admin')
admin,
admin(r'admin'),
@JsonValue(r'user')
user,
user(r'user'),
@JsonValue(r'unknown_default_open_api')
unknownDefaultOpenApi,
unknownDefaultOpenApi(r'unknown_default_open_api');

const SingleRefType(this.value);

final String value;

@override
String toString() => value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: OpenAPI API client
homepage: homepage

environment:
sdk: '>=2.15.0 <3.0.0'
sdk: '>=2.17.0 <3.0.0'

dependencies:
dio: '^5.0.0'
Expand Down