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

Generate for spec changes #96

Merged
merged 20 commits into from
Aug 19, 2023

Conversation

Nexushunter
Copy link
Contributor

@Nexushunter Nexushunter commented Aug 9, 2023

Description

Currently the openapi-generator doesn't rerun when the oas spec changes. This prevents incremental changes to the oas spec, making it a requirement to make hacky changes. To alleviate this storing a copy of the oas spec in the build directory allowing for changes to be diffed. Another option is to use git. It should also not require flutter and should allow for dart only if the pubspec doesn't include the flutter SDK as a dependency.

Boyscouting:

  • added a bunch of tests to help provide improved coverage

Acceptance Criteria

  • Generator runs on changes to the oas spec
    • Needs handle both JSON and yaml
  • Generator runs when a fresh build
  • Generator runs when a change to the config file occurs

expect(
generator.getGeneratorNameFromEnum(n.Generator.dart), equals('dart'));
});
});
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested as part of the GeneratorArguments test suite

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added new config options, added deprecations

'--type-mappings=package=type'
]));
});
test('uses config', () async {
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'm not sure how best to test this. I need to build a constantreader

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test the jarargs? Just confirming it generates the right command should be enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mostly to ensure that the expected values are injected correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bunch of refactoring

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provides a typed api to interact with through out the builder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bulk of the next gen support

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This required a lot of changes to clean up the processing of things, more work is to be done still

@gibahjoe
Copy link
Owner

Awesome work. You practically rewrote the entire library. Lol.

Will this support remote openapi specs? That is when a URL is provided as the input spec to the generator.

Also, I am unable to compile the code, getting an error

Screenshot 2023-08-12 at 16 47 58

@Nexushunter
Copy link
Contributor Author

Awesome work. You practically rewrote the entire library. Lol.

Will this support remote openapi specs? That is when a URL is provided as the input spec to the generator.

Also, I am unable to compile the code, getting an error

Screenshot 2023-08-12 at 16 47 58

Thanks 😄😅 I hope that's ok. There were a lot of things that were repeated and somewhat hard to grok but I wanted to keep support for your original workflow to allow for a nice migration path. This should still support that as the value gets passed to the openapi jar.

I will add a test to keep that support. That also brings up a good case for me to test for the load spec and caching!

If you comment out that function and the use config test block it should build again.

@Nexushunter
Copy link
Contributor Author

@gibahjoe Do you know an easy way to gather the reader? I want to ensure that the GeneratorArguments class parses the config as expected.

@gibahjoe
Copy link
Owner

Take a look at this test library which provides useful utility methods source_gen_test.

I guess, you can use this method to generate a library reader initializeLibraryReaderForDirectory and then get the constant reader from there var reader=libraryReader.annotatedWith(checker).first.annotation;

Have not tested it myself though.

…k the classes from annotations to have the toMap call, Look into why inline schema name mappings failing to produce first entry
@Nexushunter Nexushunter marked this pull request as ready for review August 13, 2023 23:29
@Nexushunter
Copy link
Contributor Author

@gibahjoe 2 things:

  • Do I need to update the commit messages?
    • I think that minor version bump will be fine as this leaves the original functionality intact while adding net new functionality that is optional.
  • It looks like there is a circular dependency in example where it needs the api/petstore as per the pubspec but it doesn't exist until the build_runner is built.
    • I can adjust this to be a nested directory but that would change the example fundamentally.

@gibahjoe gibahjoe added the docs label Aug 17, 2023
@gibahjoe
Copy link
Owner

  • Do I need to update the commit messages?

You can leave that. I will let this pr go if all other checks pass.

  • It looks like there is a circular dependency in example where it needs the api/petstore as per the pubspec but it doesn't exist until the build_runner is built.

That file is meant to be in the repo. Must have been removed by mistake. You can see the unignore line here.

Please add it back.

!api/petstore_api/pubspec.yaml

@Nexushunter
Copy link
Contributor Author

Nexushunter commented Aug 17, 2023

@gibahjoe The jar is unable to find the spec when the path exists. I've tried relative and absolute paths. Is this something you've run into? It seems like the jar is up to date

That file is meant to be in the repo. Must have been removed by mistake. You can see the unignore line here.

It's not something I removed but i believe it should get generated and I will add it back once I can get it generating again.

@gibahjoe
Copy link
Owner

gibahjoe commented Aug 17, 2023

@gibahjoe The jar is unable to find the spec when the path exists. I've tried relative and absolute paths. Is this something you've run into? It seems like the jar is up to date

@Nexushunter
No. What configuration are you using to run the generator?

@Nexushunter
Copy link
Contributor Author

I'm running flutter pub run build_runner build --delete-conflicting-outputs within the root example directory. (I had to comment out the petstore dependency to be able to flutter pub get)

@gibahjoe
Copy link
Owner

Note: New lines only included in logged output for debugging purposes

[INFO] openapi_generator on lib/main.dart:
 - :::::::::::::::::::::::::::::::::::::::::::
 - ::      Openapi generator for dart       ::
 - :::::::::::::::::::::::::::::::::::::::::::
[INFO] openapi_generator on lib/main.dart:OpenapiGenerator :: [ generate
-o api/petstore_api
-i openapi-spec.yaml
-g dart-dio
--type-mappings=Pet=ExamplePet
--additional-properties=allowUnicodeIdentifiers=false,ensureUniqueParams=true,useEnumExtension=true,prependFormOrBodyParameters=false,pubAuthor=Johnny dep..,pubName=petstore_api,legacyDiscriminatorBehavior=true,sortModelPropertiesByRequiredFlag=true,sortParamsByRequiredFlag=true,wrapper=none ]
[SEVERE] openapi_generator on lib/main.dart:
 - :: Codegen Failed. Generator output: ::
[error] The spec file is not found:  openapi-spec.yaml
[error] Check the path of the OpenAPI spec and try again.

[INFO] Running build completed, took 9.0s

This is taken from the example directory at the root of the repo (which is what I'm using)

@Openapi(
    additionalProperties:
        DioProperties(pubName: 'petstore_api', pubAuthor: 'Johnny dep..'),
    inputSpecFile: 'openapi-spec.yaml',
    typeMappings: {'Pet': 'ExamplePet'},
    generatorName: Generator.dio,
    runSourceGenOnOutput: true,
    alwaysRun: true,
    outputDirectory: 'api/petstore_api')
class MyApp extends StatelessWidget {
/// Rest of MyApp

Sorry, just seeing this. Have you figured out the issue? these config look fine to me

@Nexushunter
Copy link
Contributor Author

Nexushunter commented Aug 17, 2023

No worries!

I have not, I spent some time scouring the net and couldn't find an issue for this (outside of a docker issue, which isn't being used here). As far as I'm aware the jar is throwing the error. I also went and pulled down a new copy of the jar with no luck. I'm super confused 🤔 I will double check but I'm almost certain it's not a directory permission either.

@Nexushunter
Copy link
Contributor Author

Ok, so it seems that it is definitely something happening during the processing as I ran java -jar lib/openapi-generator.jar generate -i https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml -o api -g dart from within the openapi-generator-cli directory and it worked as expected

@Nexushunter
Copy link
Contributor Author

I found a solution. It seems like the space was being added. So I swapped out the space for an = and it generated.

@Nexushunter
Copy link
Contributor Author

@gibahjoe The checks should pass now. I was able to get it all building locally.

@Nexushunter
Copy link
Contributor Author

@gibahjoe 🎉🎉 it's finally passing so excited for this! Thanks for being so patient and responsive with everything!

@gibahjoe
Copy link
Owner

Great work. I am running a few manual checks to ensure everything is great.

Thank you for this.

@gibahjoe
Copy link
Owner

@Nexushunter

Just a few more hurdles to jump through. Kindly check my reviews.

Again, I appreciate the effort.

@gibahjoe gibahjoe self-assigned this Aug 18, 2023
@Nexushunter
Copy link
Contributor Author

@gibahjoe I'm not seeing them. Are you referring to the comment above about testing the jarArgs?

@gibahjoe
Copy link
Owner

@gibahjoe I'm not seeing them. Are you referring to the comment above about testing the jarArgs?

Not that.
Just below this event #96 (comment). I left a couple of reviews

@Nexushunter
Copy link
Contributor Author

@gibahjoe This is what I'm seeing. I did readd the pubspec as requested (which is why the flutter example passed) and the rest of the messages are about getting the config to run. Could you list out what is missing / wrong?

As an aside: I am currently looking into why there isn't as many logs coming through as I would expect.

image

Copy link
Owner

@gibahjoe gibahjoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please add some documentation that "useNextGen" flag would be removed in the future and next gen would be the default mode.

  • A modification to the annotation is still required for the sourcegen framework to trigger the openapi generator (which in turn then checks for diff between cache spec and local spec).
    Is this the desired behavior?
    For example:- If you run dart run build_runner build --delete-conflicting-outputs and the generator kicks in then make changes to your spec and run it again, the generator won't kick in again until changes are made to the annotation itself.

  • I made some changes to a spec but the generator still does not run ( I get No diff between versions, not running generator). Perhaps a regular JSON string comparison will be more thorough when checking for diffs? The specific change I made was to add a model under component schema

@gibahjoe
Copy link
Owner

@gibahjoe This is what I'm seeing. I did readd the pubspec as requested (which is why the flutter example passed) and the rest of the messages are about getting the config to run. Could you list out what is missing / wrong?

As an aside: I am currently looking into why there isn't as many logs coming through as I would expect.

image

My bad. I forgot to submit the reviews

…run. Add documentation around the usage of useNextGen, add tests
@Nexushunter
Copy link
Contributor Author

@gibahjoe I've added the work around and made the require compare and doc tweaks 🎉

@gibahjoe
Copy link
Owner

@gibahjoe I've added the work around and made the require compare and doc tweaks 🎉

Perfect. LGTM.
Looks like a check is failing. I will publish once its fixed.

You have done an awesome job.

@Nexushunter
Copy link
Contributor Author

@gibahjoe All fixed!
🤦🏻 I should really stop using Dart 3 while working with this lmao.

Thanks! I appreciate that 😄

Copy link
Owner

@gibahjoe gibahjoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great.

@gibahjoe gibahjoe merged commit 52e6821 into gibahjoe:master Aug 19, 2023
5 of 6 checks passed
@AndreBlumenthal
Copy link

Thank you for your changes. I see that you deprecated alwaysRun and overwriteExistingFiles. Do I understand this PR correctly, that we should remove them and put useNextGen to true?

@Nexushunter
Copy link
Contributor Author

@AndreBlumenthal

Np!

Yes 😄 that is the recommendation. With useNextGen it opens up a path to use incremental updates to the oas spec. It does have some current limitations around remote specs (only public are supported) but I do have a WIP to address that issue.

@AndreBlumenthal
Copy link

@AndreBlumenthal

Np!

Yes 😄 that is the recommendation. With useNextGen it opens up a path to use incremental updates to the oas spec. It does have some current limitations around remote specs (only public are supported) but I do have a WIP to address that issue.

Thank you 🙏 🙌

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.

3 participants