-
Notifications
You must be signed in to change notification settings - Fork 30
Added code related to generate webhook endpoints with different goal #1176
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
Open
RanjeethKbb
wants to merge
10
commits into
Backbase:main
Choose a base branch
from
RanjeethKbb:add_webhook_endpoints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bc788f6
changes added related to webhook prehook and posthook endpoints
RanjeethKbb 2bd30fb
Added template related to prehook, posthook and update in api templat…
RanjeethKbb a2e75d3
Reverted changes in BoatSpringCodeGen related files. Added webhooks t…
RanjeethKbb 314f6d3
Fixed generation of prehook and posthook endpoints for each request a…
RanjeethKbb 5755a91
reverted spaces in SpringCodeGen
RanjeethKbb 27489f0
reverted spaces in SpringCodeGen
RanjeethKbb f333e3d
Merge branch 'main' into add_webhook_endpoints
RanjeethKbb f6e2fcc
Updated code gen class to support all request parameters. Changed api…
RanjeethKbb 3db1bb5
Merge remote-tracking branch 'origin/add_webhook_endpoints' into add_…
RanjeethKbb b34d84a
Updated README.md with new Mojo(generate-spring-boot-embedded-webhook…
RanjeethKbb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...ven-plugin/src/main/java/com/backbase/oss/boat/GenerateSpringBootEmbeddedWebhookMojo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.backbase.oss.boat; | ||
|
|
||
| import org.apache.maven.plugin.MojoExecutionException; | ||
| import org.apache.maven.plugin.MojoFailureException; | ||
| import org.apache.maven.plugins.annotations.Mojo; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Generating Server Stubs using Spring Boot. | ||
| */ | ||
| @Mojo(name = "generate-spring-boot-embedded-webhooks", threadSafe = true) | ||
| public class GenerateSpringBootEmbeddedWebhookMojo extends AbstractGenerateMojo { | ||
|
|
||
| @Override | ||
| public void execute() throws MojoExecutionException, MojoFailureException { | ||
| getLog().info("Generating Server Stubs using Spring Boot Webhooks"); | ||
| execute("boat-webhooks", "boat-webhooks", true, false, false); | ||
| //execute("spring", "webhooks", true, false, false); | ||
| } | ||
|
|
||
| @Override | ||
| protected Collection<String> getGeneratorSpecificSupportingFiles() { | ||
| return Set.of("WebhookResponse.java", "ServletContent.java", "PosthookRequest.java", "PrehookRequest.java"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 216 additions & 0 deletions
216
boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatWebhooksCodeGen.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| package com.backbase.oss.codegen.java; | ||
|
|
||
| import io.swagger.v3.oas.models.Operation; | ||
| import io.swagger.v3.oas.models.servers.Server; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.openapitools.codegen.CliOption; | ||
| import org.openapitools.codegen.CodegenConstants; | ||
| import org.openapitools.codegen.CodegenOperation; | ||
| import org.openapitools.codegen.SupportingFile; | ||
| import org.openapitools.codegen.config.GlobalSettings; | ||
| import org.openapitools.codegen.languages.SpringCodegen; | ||
| import org.openapitools.codegen.templating.mustache.IndentedLambda; | ||
|
|
||
| import java.io.File; | ||
| import java.util.List; | ||
|
|
||
| import static org.openapitools.codegen.utils.StringUtils.camelize; | ||
|
|
||
| @Slf4j | ||
| public class BoatWebhooksCodeGen extends SpringCodegen { | ||
| public static final String NAME = "boat-webhooks"; | ||
|
|
||
| public static final String USE_CLASS_LEVEL_BEAN_VALIDATION = "useClassLevelBeanValidation"; | ||
| public static final String ADD_SERVLET_REQUEST = "addServletRequest"; | ||
| public static final String ADD_BINDING_RESULT = "addBindingResult"; | ||
| public static final String USE_LOMBOK_ANNOTATIONS = "useLombokAnnotations"; | ||
| public static final String USE_WITH_MODIFIERS = "useWithModifiers"; | ||
| public static final String USE_PROTECTED_FIELDS = "useProtectedFields"; | ||
|
|
||
| /** | ||
| * Add @Validated to class-level Api interfaces. Defaults to false | ||
| */ | ||
| @Setter | ||
| @Getter | ||
| protected boolean useClassLevelBeanValidation; | ||
|
|
||
| /** | ||
| * Adds a HttpServletRequest object to the API definition method. | ||
| */ | ||
| @Setter | ||
| @Getter | ||
| protected boolean addServletRequest; | ||
|
|
||
| /** | ||
| * Adds BindingResult to API interface method if @validate is used | ||
| */ | ||
| @Setter | ||
| @Getter | ||
| protected boolean addBindingResult; | ||
|
|
||
| /** | ||
| * Add Lombok to class-level Api models. Defaults to false | ||
| */ | ||
| @Setter | ||
| @Getter | ||
| protected boolean useLombokAnnotations; | ||
|
|
||
|
|
||
| /** | ||
| * Whether to use {@code with} prefix for pojos modifiers. | ||
| */ | ||
| @Setter | ||
| @Getter | ||
| protected boolean useWithModifiers; | ||
|
|
||
| @Setter | ||
| @Getter | ||
| protected boolean useProtectedFields; | ||
|
|
||
| public BoatWebhooksCodeGen() { | ||
| super(); | ||
| log.info("BoatWebhooksCodeGen constructor called. NAME: {}", NAME); | ||
| this.embeddedTemplateDir = this.templateDir = NAME; | ||
| this.openapiNormalizer.put("REF_AS_PARENT_IN_ALLOF", "true"); | ||
|
|
||
| this.cliOptions.add(CliOption.newBoolean(USE_CLASS_LEVEL_BEAN_VALIDATION, | ||
| "Add @Validated to class-level Api interfaces.", this.useClassLevelBeanValidation)); | ||
| this.cliOptions.add(CliOption.newBoolean(ADD_SERVLET_REQUEST, | ||
| "Adds a HttpServletRequest object to the API definition method.", this.addServletRequest)); | ||
| this.cliOptions.add(CliOption.newBoolean(ADD_BINDING_RESULT, | ||
| "Adds a Binding result as method perimeter. Only implemented if @validate is being used.", | ||
| this.addBindingResult)); | ||
| this.cliOptions.add(CliOption.newBoolean(USE_LOMBOK_ANNOTATIONS, | ||
| "Add Lombok to class-level Api models. Defaults to false.", this.useLombokAnnotations)); | ||
| this.cliOptions.add(CliOption.newBoolean(USE_WITH_MODIFIERS, | ||
| "Whether to use \"with\" prefix for POJO modifiers.", this.useWithModifiers)); | ||
| this.cliOptions.add(CliOption.newString(USE_PROTECTED_FIELDS, | ||
| "Whether to use protected visibility for model fields")); | ||
| supportedLibraries.put("boat-webhooks", "Boat Webhooks codegen"); | ||
| this.apiNameSuffix = "Api"; | ||
| this.apiNamePrefix = "Webhook"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public String toApiName(String name) { | ||
| if (name.isEmpty()) { | ||
| name = "default"; | ||
| } | ||
|
|
||
| name = sanitizeName(name); | ||
|
|
||
| return camelize(this.apiNamePrefix + "_" + name + "_" + this.apiNameSuffix); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void processOpts() { | ||
| super.processOpts(); | ||
| log.info("BoatWebhooksCodeGen processOpts called. Adding supporting files and properties."); | ||
|
|
||
| // Whether it's using ApiUtil or not. | ||
| // cases: | ||
| // <supportingFilesToGenerate>ApiUtil.java present or not</supportingFilesToGenerate> | ||
| // <generateSupportingFiles>true or false</generateSupportingFiles> | ||
| final String supFiles = GlobalSettings.getProperty(CodegenConstants.SUPPORTING_FILES); | ||
| // cleared by <generateSuportingFiles>false</generateSuportingFiles> | ||
| final boolean useApiUtil = supFiles != null && (supFiles.isEmpty() | ||
| ? needApiUtil() // set to empty by <generateSuportingFiles>true</generateSuportingFiles> | ||
| : supFiles.contains("ApiUtil.java")); // set by <supportingFilesToGenerate/> | ||
|
|
||
| if (!useApiUtil) { | ||
| this.supportingFiles | ||
| .removeIf(sf -> "apiUtil.mustache".equals(sf.getTemplateFile())); | ||
| } | ||
|
|
||
| writePropertyBack("useApiUtil", useApiUtil); | ||
|
|
||
| // Adding Webhook related models to supporting files | ||
| final var webhookResponseTemplate = "WebhookResponse"; | ||
| this.supportingFiles.add(new SupportingFile(webhookResponseTemplate + ".mustache", | ||
| (sourceFolder + File.separator + modelPackage).replace(".", java.io.File.separator), | ||
| webhookResponseTemplate + ".java")); | ||
|
|
||
| final var servletContentTemplate = "ServletContent"; | ||
| this.supportingFiles.add(new SupportingFile(servletContentTemplate + ".mustache", | ||
| (sourceFolder + File.separator + modelPackage).replace(".", java.io.File.separator), | ||
| servletContentTemplate + ".java")); | ||
|
|
||
| final var posthookRequestTemplate = "PosthookRequest"; | ||
| this.supportingFiles.add(new SupportingFile(posthookRequestTemplate + ".mustache", | ||
| (sourceFolder + File.separator + modelPackage).replace(".", java.io.File.separator), | ||
| posthookRequestTemplate + ".java")); | ||
|
|
||
| final var prehookRequestTemplate = "PrehookRequest"; | ||
| this.supportingFiles.add(new SupportingFile(prehookRequestTemplate + ".mustache", | ||
| (sourceFolder + File.separator + modelPackage).replace(".", java.io.File.separator), | ||
| prehookRequestTemplate + ".java")); | ||
| String modelPath = (sourceFolder + File.separator + modelPackage).replace(".", java.io.File.separator); | ||
| log.info("Supporting file output path: {}", modelPath); | ||
| this.importMapping.put(webhookResponseTemplate, modelPackage + "." + webhookResponseTemplate); | ||
| this.importMapping.put(servletContentTemplate, modelPackage + "." + servletContentTemplate); | ||
| this.importMapping.put(posthookRequestTemplate, modelPackage + "." + posthookRequestTemplate); | ||
| this.importMapping.put(prehookRequestTemplate, modelPackage + "." + prehookRequestTemplate); | ||
|
|
||
| log.info("supportingFiles size: {}", this.supportingFiles.size()); | ||
|
|
||
| log.info("supportingFiles size: {}", this.supportingFiles); | ||
|
|
||
| if (this.additionalProperties.containsKey(USE_CLASS_LEVEL_BEAN_VALIDATION)) { | ||
| this.useClassLevelBeanValidation = convertPropertyToBoolean(USE_CLASS_LEVEL_BEAN_VALIDATION); | ||
| } | ||
| if (this.additionalProperties.containsKey(ADD_SERVLET_REQUEST)) { | ||
| this.addServletRequest = convertPropertyToBoolean(ADD_SERVLET_REQUEST); | ||
| } | ||
| if (this.additionalProperties.containsKey(ADD_BINDING_RESULT)) { | ||
| this.addBindingResult = convertPropertyToBoolean(ADD_BINDING_RESULT); | ||
| } | ||
| if (this.additionalProperties.containsKey(USE_LOMBOK_ANNOTATIONS)) { | ||
| this.useLombokAnnotations = convertPropertyToBoolean(USE_LOMBOK_ANNOTATIONS); | ||
| } | ||
| if (this.additionalProperties.containsKey(USE_WITH_MODIFIERS)) { | ||
| this.useWithModifiers = convertPropertyToBoolean(USE_WITH_MODIFIERS); | ||
| } | ||
| if (this.additionalProperties.containsKey(USE_PROTECTED_FIELDS)) { | ||
| this.additionalProperties.put("modelFieldsVisibility", "protected"); | ||
| } else { | ||
| this.additionalProperties.put("modelFieldsVisibility", "private"); | ||
| } | ||
|
|
||
| writePropertyBack(USE_CLASS_LEVEL_BEAN_VALIDATION, this.useClassLevelBeanValidation); | ||
| writePropertyBack(ADD_SERVLET_REQUEST, this.addServletRequest); | ||
| writePropertyBack(ADD_BINDING_RESULT, this.addBindingResult); | ||
| writePropertyBack(USE_LOMBOK_ANNOTATIONS, this.useLombokAnnotations); | ||
| writePropertyBack(USE_WITH_MODIFIERS, this.useWithModifiers); | ||
| writePropertyBack(USE_PROTECTED_FIELDS, this.useProtectedFields); | ||
|
|
||
| this.additionalProperties.put("indent4", new IndentedLambda(4, " ", true, true)); | ||
| this.additionalProperties.put("newLine4", new BoatSpringCodeGen.NewLineIndent(4, " ")); | ||
| this.additionalProperties.put("indent8", new IndentedLambda(8, " ", true, true)); | ||
| this.additionalProperties.put("newLine8", new BoatSpringCodeGen.NewLineIndent(8, " ")); | ||
| this.additionalProperties.put("toOneLine", new BoatSpringCodeGen.FormatToOneLine()); | ||
| this.additionalProperties.put("trimAndIndent4", new BoatSpringCodeGen.TrimAndIndent(4, " ")); | ||
| } | ||
|
|
||
| private boolean needApiUtil() { | ||
| return this.apiTemplateFiles.containsKey("api.mustache") | ||
| && this.apiTemplateFiles.containsKey("apiDelegate.mustache"); | ||
| } | ||
|
|
||
| @Override | ||
| public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) { | ||
| final CodegenOperation codegenOperation = super.fromOperation(path, httpMethod, operation, servers); | ||
| // Remove the standard body parameter (if it exists) --- | ||
| // This prevents the generator's default logic from inserting its own request body. | ||
| codegenOperation.allParams.removeIf(p -> p.isBodyParam); | ||
| return codegenOperation; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
boat-scaffold/src/main/templates/boat-spring/PosthookRequest.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package {{modelPackage}}; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Predefined request object for posthook endpoints containing API response and the request details. | ||
| */ | ||
| public class PosthookRequest extends ServletContent { | ||
|
|
||
| private PrehookRequest requestDetails; | ||
|
|
||
| public PosthookRequest body(String body) { | ||
| this.setBody(body); | ||
| return this; | ||
| } | ||
|
|
||
| public PosthookRequest headers(Map<String, List<String>> headers) { | ||
| this.setHeaders(headers); | ||
| return this; | ||
| } | ||
|
|
||
| public PosthookRequest parameters(Map<String, String[]> parameters) { | ||
| this.setParameters(parameters); | ||
| return this; | ||
| } | ||
|
|
||
| public PosthookRequest requestDetails(PrehookRequest requestDetails) { | ||
| this.requestDetails = requestDetails; | ||
| return this; | ||
| } | ||
|
|
||
| public PrehookRequest getRequestDetails() { | ||
| return requestDetails; | ||
| } | ||
|
|
||
| public void setRequestDetails(PrehookRequest requestDetails) { | ||
| this.requestDetails = requestDetails; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| PosthookRequest posthookRequest = (PosthookRequest) o; | ||
| return super.equals(o) && Objects.equals(this.requestDetails, posthookRequest.requestDetails); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return super.hashCode() + Objects.hash(requestDetails); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append("class PosthookRequest {\n"); | ||
| sb.append(" body: ").append(toIndentedString(getBody())).append("\n"); | ||
| sb.append(" headers: ").append(toIndentedString(getHeaders())).append("\n"); | ||
| sb.append(" parameters: ").append(toIndentedString(getParameters())).append("\n"); | ||
| sb.append(" requestDetails: ").append(toIndentedString(requestDetails)).append("\n"); | ||
| sb.append("}"); | ||
| return sb.toString(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is a new Mojo, there should be some documentation somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure @jjjasper, I will add documentation about new Mojo in readme file.