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

Conversation

AAkira
Copy link
Contributor

@AAkira AAkira commented May 2, 2023

Fix #15382

I implemented toString in Enum on dart-dio, which can be used from Dart 2.17.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    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.
  • In case you are adding a new generator, run the following additional script :
    ./bin/utils/ensure-up-to-date.sh
    
    Commit all changed files.
  • File the PR against the correct branch: master (6.3.0) (minor release - breaking changes with fallbacks), 7.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@AAkira AAkira changed the title [dart-dio] json serializer enum [dart-dio] Add toString in Enum on json_serializer May 2, 2023
@ahmednfwela
Copy link
Contributor

@jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12) @ahmednfwela (2021/08)

@kuhnroyal
Copy link
Contributor

What are the general thoughts on bumping Dart to 2.17?

@ahmednfwela
Copy link
Contributor

I think it would be fine really

@ahmednfwela
Copy link
Contributor

ahmednfwela commented May 7, 2023

can we limit this version bump for json_serializer only?

@AAkira
Copy link
Contributor Author

AAkira commented May 8, 2023

I'm sorry.
I made a mistake in a non-string conversion and fixed it.

@AAkira
Copy link
Contributor Author

AAkira commented May 8, 2023

@ahmednfwela

can we limit this version bump for json_serializer only?

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

@DevNico
Copy link

DevNico commented Oct 17, 2023

Can this pr get some attention please we're also dependent on this change.

@vasilich6107
Copy link
Contributor

Hi seniors!
Is there anything that we could help with?

@defint
Copy link

defint commented Nov 1, 2023

Hi @ahmednfwela
As far as I see @kuhnroyal approved the PR.
Could you clarify if there are any issues left that prevents it from merging?
We are struggling without this feature.

Copy link
Contributor

@ahmednfwela ahmednfwela left a 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

@wing328

@wing328 wing328 merged commit 6425fbe into OpenAPITools:master Nov 2, 2023
@wing328 wing328 added this to the 7.1.0 milestone Nov 2, 2023
@DevNico
Copy link

DevNico commented Nov 20, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] [dart-dio] Enum class does not implement toString using json_serializer
7 participants