asyncapi/java-spring-template

Ability to only generate model classes

Closed this issue · 12 comments

Reason/Context

Please try answering few of those questions

  • Why we need this improvement?
    Currently, the generator only works for greenfield projects. If I already have a project and adding in a new AsyncAPI, I need to do this in another directory and manually copy files over.

  • How will this change help?
    Can make the generation of the model part of mvn and follow standard target/generated-sources flow

  • What is the motivation?
    I only need the model for my purposes. Most other spec -> generators support this flow (i.e.: swagger/openapi)

Description

Please try answering few of those questions

  • What changes have to be introduced?
    Only the model classes should be generated, everything else would be skipped.

  • Will this be a breaking change?
    I'd expect not, as it would be another param which controls the flow

  • How could it be implemented/designed?
    Not sure, I had a quick look but it seems the generators copy files over and then process them. would need the ability to control what is copied into the target directory.

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.

Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

Hey @garethahealy. That is a completely valid use case. We do not have a single recommended approach here atm.
There are different options:

  • long term create a separate "template" project with models only
  • we could add some flag here that would support what you need, like -p modelsOnly=true

The preferred future approach is to base model generation on our new project https://github.com/asyncapi/modelina that will be integrated with our CLI, so you can explicitly ask the CLI for models only. This is not going to happen very quick though.

I think the best would be to just add -p modelsOnly=true. If you want to work on this, the work should not be complicated, except for documentation you would need to provide a simple hook:

@derberg ; i've already gone down the custom template route: https://github.com/garethahealy/java-spring-template

re: 02_removeNotRelevantParts.js - I've also tried this way, but I need to hook into the before phase and change the template dir, i.e.: remove the gradle files.

I couldn't see how to do that via: https://github.com/asyncapi/generator/blob/c369b8542ab6791546e864664ce0eba14ed7942b/lib/generator.js#L102-L129

any ideas?

If I do it in the after phase, the template might overwrite files or I might remove files I shouldnt.

if you want to hook in before then just name your hook 'generate:before' instead of 'generate:after'. For more read this.

If I do it in the after phase, the template might overwrite files or I might remove files I shouldn't.

just to understand your flow, you use this template to bootstrap the initial service, and over time you want to regenerate only data models? I thought you need data models only

I do only want the models. Typically, I'll bootstrap my project via mvn; in this case, it's an apache camel project. ideally, I'd like to hook up the ag command via mvn exec so the models are generated on every build, but that's the long term end-goal.

Just to clarify the phases comment; the template includes files (i.e.: gradle) which I cant see how to stop being included via the hooks, because I don't see a way in the before stage of changing what's in the template dir or deciding what should be written to the target dir. If it's in the template dir, it gets added to the targetDir - how can a generator via the generate:before exclude a file?

Does that make sense?

how can a generator via the generate:before exclude a file?

did you try to check if you have access to templateContentDir, it should point to template and its template directory, and therefore you should be able to remove template files from there before generation process.

before using templateContentDir it would be good to first add it in the generator project as official type so it is tracked for backward compatibility.

this could even turn out to be a generic hook that anyone can later include in their templates. You could do this in the way that user can pass a simple parameter like ignoreGeneration or something that would be a comma-separated list of paths (files and dirs) that should be ignored during generation.


another solution -> make it a generic feature of the generator. We already have there an option in the library to provide entrypoint to generate only a single file, so why not having an option to specify a list of files to ignore. I didn't read generator code in some time but I think is would be as easy as just accept a list of files and dirs as array on input and then ignore files that match here

it would partially solve my problem from asyncapi/generator#581

or extend entrypoint to support not only file but dir too, and then just enable it on the CLI 🤔

There are few options, hook would be just a workaround

Hi i done this in the following branch of my fork https://github.com/micromata/java-spring-template/tree/feature/only_generate_model

You only have to add -p onlyModel=true

This branch also includes the possibility to use the discrimnator wit the allOf Pattern.

I will open a PR soon but feel free to check it out

This issue has been automatically marked as stale because it has not had recent activity 😴
It will be closed in 60 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation.
Thank you for your contributions ❤️

@tuxBurner best would be to check your work on a PR. Feel free to open just a draft PR for review

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@tuxBurner @garethahealy any of you interested in opening a PR. Otherwise I will close as such stuff should be done with https://github.com/asyncapi/modelina imho

@derberg ; I dont have the time currently to do a PR, so happy for it to be closed.