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

Output is invalid #5

Closed
phackwer opened this issue Dec 9, 2022 · 26 comments · Fixed by #6
Closed

Output is invalid #5

phackwer opened this issue Dec 9, 2022 · 26 comments · Fixed by #6
Labels
bug Something isn't working enhancement New feature or request

Comments

@phackwer
Copy link
Contributor

phackwer commented Dec 9, 2022

Hi,

The output we had with your code became a bit invalid.

It's missing a .g.dart file and also missing the a private class:

image

Also the dependencies are not totally clear, could you map them? The output however, seems quite nice.

@Carapacik
Copy link
Owner

Carapacik commented Dec 9, 2022

Hello @phackwer, thanks for this issue.
In order for the part translation_controller.g.dart and _TranslationControllerClient errors to disappear, you need:

  1. write dependencies like in example in your pubspec.yaml file like these:
dependencies:
  dio: ^4.0.0
  json_annotation: ^4.7.0
  retrofit: ^3.3.0

dev_dependencies:
  build_runner: ^2.3.0
  json_serializable: ^6.5.0
  retrofit_generator: ^4.2.0
  swagger_parser: ^0.3.0
  1. run code generarion with command
flutter pub run build_runner build

And for type object in Future in need your full swagger.json file or piece of your swagger'.json file with this request

@phackwer
Copy link
Contributor Author

phackwer commented Dec 9, 2022

So, I still have problems with what is in the Controller. There it's also missing a .g.dart on each one of them. The above solution fixed the problems with the Models only (they were also missing the json serializers, now they have it).

image

@Carapacik
Copy link
Owner

Try to run step-by-step
flutter pub get
flutter pub run swagger_parser:generate
flutter pub run build_runner build --delete-conflicting-outputs

If this doesn't work try to restart dart analysis server.

@Carapacik
Copy link
Owner

Carapacik commented Dec 9, 2022

So, I still have problems with what is in the Controller. There it's also missing a .g.dart on each one of them. The above solution fixed the problems with the Models only (they were also missing the json serializers, now they have it).

image

Can you show output what it gives out when generating build_runner code?

@phackwer
Copy link
Contributor Author

phackwer commented Dec 9, 2022

Same result. :-( No generated file 'translation_controller.g.dart'.

@phackwer
Copy link
Contributor Author

phackwer commented Dec 9, 2022

I had some errors in output, could they be causing the problem?

[SEVERE] retrofit_generator:retrofit on lib/generated/clients/user_controller_api_client.dart (cached):

This builder requires Dart inputs without syntax errors.
However, package:swagger_parser_api/generated/clients/user_controller_api_client.dart (or an existing part) contains the following errors.
user_controller_api_client.dart:38:22: Expected to find ';'.
user_controller_api_client.dart:38:35: Expected a class member.

Try fixing the errors and re-running the build.

[SEVERE] retrofit_generator:retrofit on lib/generated/clients/translation_controller_api_client.dart (cached):

Exception: toJson() method have to add to MultipartFile
[SEVERE] retrofit_generator:retrofit on lib/generated/shared_models/language_package.dart (cached):

This builder requires Dart inputs without syntax errors.
However, package:swagger_parser_api/generated/shared_models/language_package.dart (or an existing part) contains the following errors.
language_package.dart:14:10: Expected an identifier.
language_package.dart:14:10: Expected to find '}'.
language_package.dart:21:9: Expected to find ';'.
And 6 more...

@Carapacik
Copy link
Owner

Show how your translation_controller.dart file looks like.
And try to fix other issues in your code than generate again.

@Carapacik
Copy link
Owner

Carapacik commented Dec 9, 2022

Exception: toJson() method have to add to MultipartFile [SEVERE] retrofit_generator:retrofit on lib/generated/shared_models/language_package.dart (cached):

Try to replace all MultipartFile types to List<MultipartFile> and run code generation again, this should help.

@phackwer
Copy link
Contributor Author

phackwer commented Dec 9, 2022

We found some issues here related to oneOf, allOf lists of types for arrays. Regarding the MultipartFile problem is that this class was not generated (body it had body type string and format binary, so, we removed it from the open api definition and it stopped causing an error.

image

This error with the List being created could be solved by simple making type "List" whenever oneOf or allOf (or anything like this) was presented. A better solution would be to create a bogus interface for the list "List" that would be implemented by the classes that are part of the lists.

@phackwer
Copy link
Contributor Author

phackwer commented Dec 9, 2022

We end on this error here and from there, no .g.dart is created for the controllers.

image

@Carapacik
Copy link
Owner

I've never met oneOf, I don't know if it's supported in Dart. If there was a complete example and a version, maybe something would have been done. And so I don't even have a clue how to fix it.

@Carapacik
Copy link
Owner

Carapacik commented Dec 9, 2022

We end on this error here and from there, no .g.dart is created for the controllers.

image

What type should this list have in your case?

@Carapacik
Copy link
Owner

I will try in my free time to look at anyOf and oneOf. It would be great if you threw off an example on which it is used and what should happen.

@phackwer
Copy link
Contributor Author

phackwer commented Dec 9, 2022

Here is documentation about it:

https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/

It basically allows you to define a property on complex class as array and say that items on that array can be one of, all of or any of certain types.

Dart doesn't support it, this is why it would be better either to have those cases with no type, or create an empty interface forcing all classes to implement.

Ex:

ShoppingCart has property items, so, you would have

class ShoppingCart{
List items;
}

If items has anyOf SomeItem or SomeItem2, you would have

class ShoppingCart{
List<InterfaceShoppingCartItems> items;
}

abstract class InterfaceShoppingCartItems{}

class SomeItem implements InterfaceShoppingCartItems...

class SomeItem2 implements InterfaceShoppingCartItems...

If items has oneOf, you would have

class ShoppingCart{
InterfaceShoppingCartItem item;
}

and so on.

@phackwer
Copy link
Contributor Author

phackwer commented Dec 9, 2022

Also a tiny bug we found:

type: object

It should revert to Object on code generated. We had

final object something;

and that caused errors too.

@Carapacik
Copy link
Owner

Carapacik commented Dec 9, 2022

Here is documentation about it:

https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/

It basically allows you to define a property on complex class as array and say that items on that array can be one of, all of or any of certain types.

Dart doesn't support it, this is why it would be better either to have those cases with no type, or create an empty interface forcing all classes to implement.

Ex:

ShoppingCart has property items, so, you would have

class ShoppingCart{
List items;
}

If items has anyOf SomeItem or SomeItem2, you would have

class ShoppingCart{
List<InterfaceShoppingCartItems> items;
}

abstract class InterfaceShoppingCartItems{}

class SomeItem implements InterfaceShoppingCartItems...

class SomeItem2 implements InterfaceShoppingCartItems...

If items has oneOf, you would have

class ShoppingCart{
InterfaceShoppingCartItem item;
}

and so on.

It looks great, you can try to implement it in the next version

Also a tiny bug we found:

type: object

It should revert to Object on code generated. We had

final object something;

and that caused errors too.

Can you throw off a piece of this json file with this field, perhaps there is also one of the moments unknown to the program?

@Carapacik
Copy link
Owner

Carapacik commented Dec 9, 2022

Take a look at this, it might come in handy:
https://pub.dev/packages/freezed#fromjson---classes-with-multiple-constructors

@phackwer
Copy link
Contributor Author

phackwer commented Dec 9, 2022

I can't throw much here of the json I have, but here is small example:

image

Backend can return any type of objects in some cases.

Dart does accept type "Object", so, in this case would be simple: type object is Object.

@phackwer
Copy link
Contributor Author

phackwer commented Dec 9, 2022

Take a look at this, it might come in handy: https://pub.dev/packages/freezed#fromjson---classes-with-multiple-constructors

Already know it, thanks. :-)

@Carapacik
Copy link
Owner

Carapacik commented Dec 9, 2022

If you had shown a little lower, because there may be different variables in the object type. For example:

@Carapacik
Copy link
Owner

There isn't: backend is Java, it's just mapping a generic Object as response. So the output end being "object" lowercase.

Without any properties?

@phackwer
Copy link
Contributor Author

phackwer commented Dec 9, 2022

image

There isn't: backend is Java, it's just mapping a generic Object as response. So the output end being "object" lowercase.

@Carapacik
Copy link
Owner

If you change object to Object will it work?

@phackwer
Copy link
Contributor Author

phackwer commented Dec 9, 2022

JSON that generated this is close to this:

     Response": {
        "type": "object",
        "properties": {
          "general": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          ...
          }

@Carapacik
Copy link
Owner

What version of swagger is used?

@phackwer
Copy link
Contributor Author

phackwer commented Dec 9, 2022

Open API ^3.0

@Carapacik Carapacik linked a pull request Dec 12, 2022 that will close this issue
@Carapacik Carapacik added the bug Something isn't working label Mar 30, 2023
@Carapacik Carapacik added the enhancement New feature or request label Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants