openapi-generator
openapi-generator copied to clipboard
[BUG][DART] The language version override has to be the same in the library and its part(s).
Description
I ran into a dart versioning problem. My project has sdk: '>=2.17.0 <3.0.0', and openapi-cli generated API has sdk: '>=2.14.0 <3.0.0'. I can not downgrade my project SDK version, but when I manually change SDK version in "generated_api/pubscpec.yaml", the project builds without errors. I search possibilities to set up a custom SDK version for openapi-generator-cli before generating API, but found nothing. Is there any solution for manually or automatically determining the SDK version to avoid such a problem?
openapi-generator version
// https://mvnrepository.com/artifact/org.openapitools/openapi-generator-cli compileOnly 'org.openapitools:openapi-generator-cli:6.3.0'
OpenAPI declaration file content or url
My declaration JSON file without errors, I can't share it due to my company's policy.
Generation Details
java -jar C:\Users\DEV\Documents\openapi-generator-cli.jar generate -i https://test-example.com/api/v2/api-docs -g dart-dio -o C:\Users\DEV\Documents\Projects\mobile\lib\api -p pubLibrary=mobile.api,pubName=mobile_api,serializationLibrary=json_serializable
Steps to reproduce
Set up in project's pubspec.yaml file SDK version like this: environment: sdk: '>=2.17.0 <3.0.0'
Then generate API via openapi-generator-cli. flutter pub get flutter pub run build_runner build --delete-conflicting-outputs
Build app, then in the debug logs you will see "The language version override has to be the same in the library and its part(s)." error.
Suggest a fix
Add the ability to manually or automatically determine the SDK version.
Im pretty sure that this isnt the problem. I had the same problem a moment ago, but could fix it with flutter upgrade, flutter pub get. Setting the sdk version manually didnt fix it for me.
EDIT: the problem isnt the sdk, but the json_anotation. The versions needs to be the same. It could be fixed with sed -i -e 's/4.4.0/4.8.0/g' ./pubspec.yaml to update and flutter pub get. This will just replace the version 4.4.0 to 4.8.0. You could do the same for the sdk if you want to.
@JustFrederik sorry, I didn't understand how it should be fixed. Should I add -i -e parameters when generating API files via openapi-generator-cli?
You can replace text in a file with sed. The problem is that some version of openapi is interfering with your main project. Just set the versions with replace before building the openapi library. An example would be a shellscript like this:
java -jar C:\Users\DEV\Documents\openapi-generator-cli.jar generate -i https://test-example.com/api/v2/api-docs -g dart-dio -o C:\Users\DEV\Documents\Projects\mobile\lib\api -p pubLibrary=mobile.api,pubName=mobile_api,serializationLibrary=json_serializable cd generated || exit flutter pub upgrade --major-versions sed -i -e 's/4.4.0/4.8.0/g' ./pubspec.yaml sed -i -e 's/>=2.17.0 <3.0.0/>=2.19.4 <3.0.0/g' ./pubspec.yaml flutter pub get flutter pub run build_runner build --delete-conflicting-outputs
if you find a more elegant way to update every version to the newest version let me know. If you create a docker image for the generator, the versions of the openapi library will stay the same and the shell script will work indefinitely
@JustFrederik I've tried your solution and it works perfectly. Thanks for helping! As you said, we will wait for a more elegant decision from the developers to change the version in generated/pubspec.yaml automatically.
@jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12) @ahmednfwela (2021/08)
Fix it please!!! Same error: Error: The language version override has to be the same in the library and its part(s).
jar: openapi-generator-cli-7.3.0.jar
pubspec.yaml: environment: sdk: '>=3.2.3 <4.0.0'
pubspec.yaml (GENERATED): environment: sdk: '>=2.15.0 <3.0.0'
This isn't an issue with openapi-generator, but with the way you structure the generated code. What worked for me is moving the generated code outside the lib/ folder (thus making openapi lib language version independent from the library language version):
pubspec.yaml:
zzz_api:
path: ./dependencies/_zzz_api
This way it works with different language versions in the app/library and the generated openapi library.