-
Notifications
You must be signed in to change notification settings - Fork 124
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
Cannot generate code from specific swagger 2.0 spec. Not sure why it's failing #189
Comments
Hi @point-source , I will check this issue ASAP |
@point-source I can't open this link Can you please share this swagger file? |
Strange, I seem to be able to access it without any credentials/auth. What kind of error do you get? I have also added it as a gist here: https://gist.github.com/point-source/5ac94a577ff56c6d48de0682b3739d53 |
@point-source thanks, I'll check it tomorrow! |
THis one fails because your models has "+" symbols in names :D Working on fix |
I actually noticed this and did a quick find/replace and replaced them with '-'. I also filed a bug report with the provider of this spec since it is non-compliant with the openapi official spec. This fix allowed the models to build (with the build only models option) but the chopper classes still fail if I disable that option. |
@point-source I'll let you know when this file will generated without errors. Just wait a little. Already working ot in |
Sure thing. Thank you so much for the excellent support here! |
Ok now your file generated well! I will check my changes on all my beatifull collection of swaggers and if everything is OK - release 2.0.9 Now you can use git ref in pubspec.yaml swagger_dart_code_generator: |
Testing now. Generation looks good, no errors. Trying to actually test against the api right now and getting a decode error. Trying to figure out if that's on my side or not. Just for info, the error is: _TypeError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FwCoreControllersFwJwtControllerJwtResponseModel?') Occuring on the return line (copyWith) of decodeJson method of the chopper JsonConverter class. I have confirmed the actual returned json from the server has matching keys with the model it is trying to convert to. |
Ah yep, looks like the replacement of the JsonSerializableConverter() with chopper.JsonConverter() is the culprit. Specifically the removal of this code from generation: typedef JsonFactory<T> = T Function(Map<String, dynamic> json);
class CustomJsonDecoder {
CustomJsonDecoder(this.factories);
final Map<Type, JsonFactory> factories;
dynamic decode<T>(dynamic entity) {
if (entity is Iterable) {
return _decodeList<T>(entity);
}
if (entity is T) {
return entity;
}
if (entity is Map<String, dynamic>) {
return _decodeMap<T>(entity);
}
return entity;
}
T _decodeMap<T>(Map<String, dynamic> values) {
final jsonFactory = factories[T];
if (jsonFactory == null || jsonFactory is! JsonFactory<T>) {
return throw "Could not find factory for type $T. Is '$T: $T.fromJsonFactory' included in the CustomJsonDecoder instance creation in bootstrapper.dart?";
}
return jsonFactory(values);
}
List<T> _decodeList<T>(Iterable values) =>
values.where((v) => v != null).map<T>((v) => decode<T>(v) as T).toList();
}
class JsonSerializableConverter extends chopper.JsonConverter {
@override
chopper.Response<ResultType> convertResponse<ResultType, Item>(
chopper.Response response) {
if (response.bodyString.isEmpty) {
// In rare cases, when let's say 204 (no content) is returned -
// we cannot decode the missing json with the result type specified
return chopper.Response(response.base, null, error: response.error);
}
final jsonRes = super.convertResponse(response);
return jsonRes.copyWith<ResultType>(
body: jsonDecoder.decode<Item>(jsonRes.body) as ResultType);
}
}
final jsonDecoder = CustomJsonDecoder(LassoJsonDecoderMappings); Not saying we need to bring that code back but it does seem that they are not equivalent substitutes at the moment. After using the old custom converter, the generated code appears to work. |
Additionally, out of the 9 swagger files I have from this application, your fix has worked on 3 of them. Here are all of them so you can add them to your testing collection: https://www.dropbox.com/sh/2t3s99zsmgf89ar/AACGJzjw3cPRCCEMDMvn6is9a?dl=0 The ones that are working are:
Let me know how I can help here. Thanks again |
Cool, I'll check it |
I am having an issue parsing this spec: https://webapidemo.rentalworks.net/swagger/accountservices-v1/swagger.json
In fact, pretty much every spec from this site has issues with this generator. I am assuming there is something wrong with the spec itself but I'm having a hard time figuring out what since I'm not familiar with how these code generators currently work. It looks like it is incorrectly parsing. Any help would be appreciated. Thanks!
Build runner output:
The text was updated successfully, but these errors were encountered: