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

Add support in GroupedOpenApi to match api's based on java annotations #2812

Open
ianwallen opened this issue Dec 13, 2024 · 0 comments
Open

Comments

@ianwallen
Copy link
Contributor

Is your feature request related to a problem? Please describe.
We currently use GroupedOpenApi to define several grouped api.
We have been mostly creating package to identify each group. But we have a lot of api's that are shared between the packages. So we end up doing abstract classes and then extend them in the package so that the common code is shared.
This works however as we get more and more packages, it is getting more difficult to manage the differences.

Describe the solution you'd like

There are currently many option to identifying the api's to be included in the group such as packageToScan, pathToInclude, ....
I would like to propose a new one that is based on the java annotations.

i.e. annotationsToMatch()

It could include the list of java annotation to match which could be used to identify the api's to be included in the group specificaion.

@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.TYPE, ElementType.METHOD}) 
   public @interface App1Annotation {
}

@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.TYPE, ElementType.METHOD}) 
   public @interface App2Annotation {
}
@Bean
public GroupedOpenApi app1Api() {
	return GroupedOpenApi.builder()
               .setGroup("app1")
               .annotationsToMatch(app1Annotation) // Proposed new method
 	       .build();
}

@Bean
public GroupedOpenApi app2Api() {
	return GroupedOpenApi.builder()
               .setGroup("app2")
               .annotationsToMatch(app2Annotation) // Proposed new method
 	       .build();
}
@App1Annotation
@GetMapping(value = "/app1api")
public Result<Void> getApp1Data() {
    // Code here...
}

@App1Annotation
@App2Annotation
@GetMapping(value = "/app1andapp2api")
public Result<Void> getData() {
    // Code here...
}

In this example app1 would have both apis
But app2 would only have the /app1andapp2api api.

Describe alternatives you've considered

It may be possible using other method like the packageToScan and pathToInclude but it seems like this creates messy code. Using annotations seems to be cleaner option.

Additional context

To be consistent with other functions,

  • annotationsToMatch() should probably accept a list of annotations in case there are multiple.
  • annotationsToMatch() could be applied to annotations on classes (to include the full class) or on methods (to be specific to one method).
  • There should probably be an annotationsToExclude() to also be consistent with existing methods.
  • If we don't want to use third party reflections class to scan all annotations then the method may need a base class for package to scan for the annotations so method may be something like this,
    annotationsToMatch(List<String> packageNames, Class<? extends Annotation>... annotations);
    Or
    annotationsToMatch(List<String> packageNames, List<Class<? extends Annotation>> annotations)
    Or
    packagesToScan(String... packagesToScan).annotationsToMatch(Class<? extends Annotation>... annotations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant