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

Spring: Api Objects in methods annotated with @GetMapping @PostMapping #244

Open
mschaffl opened this issue Apr 27, 2018 · 2 comments
Open

Comments

@mschaffl
Copy link

I observed that ApiObjects are only detected in controllers whichs methods are annotated with @RequestMapping.
They are not detected in controllers which are using @GetMapping, PostMapping ... instead.

@mschaffl mschaffl changed the title Spring: Api Objects in mehtods annotated with @GetMapping @PostMapping Spring: Api Objects in methods annotated with @GetMapping @PostMapping Apr 27, 2018
@yaosing
Copy link

yaosing commented Feb 28, 2019

Hello, I also meet this problem. After debug I found that this is caused by:
AbstractSpringJSONDocScanner class's jsondocObjects method just get methods with RequestMapping annotation.
Hope someone can fix this problem! Thanks.
By the way my version is 1.2.19.
Below is the AbstractSpringJSONDocScanner class's jsondocObjects method code:

@Override
	public Set<Class<?>> jsondocObjects(List<String> packages) {
		Set<Method> methodsAnnotatedWith = reflections.getMethodsAnnotatedWith(RequestMapping.class);
		Set<Class<?>> candidates = Sets.newHashSet();
		Set<Class<?>> subCandidates = Sets.newHashSet();
		Set<Class<?>> elected = Sets.newHashSet();
		
		for (Method method : methodsAnnotatedWith) {
			buildJSONDocObjectsCandidates(candidates, method.getReturnType(), method.getGenericReturnType(), reflections);
			Integer requestBodyParameterIndex = JSONDocUtils.getIndexOfParameterWithAnnotation(method, RequestBody.class);
			if(requestBodyParameterIndex != -1) {
				candidates.addAll(buildJSONDocObjectsCandidates(candidates, method.getParameterTypes()[requestBodyParameterIndex], method.getGenericParameterTypes()[requestBodyParameterIndex], reflections));
			}
		}
		
		// This is to get objects' fields that are not returned nor part of the body request of a method, but that are a field
		// of an object returned or a body  of a request of a method
		for (Class<?> clazz : candidates) {
			appendSubCandidates(clazz, subCandidates, reflections);
		}

		candidates.addAll(subCandidates);
		
		for (Class<?> clazz : candidates) {
			if(clazz.getPackage() != null) {
				for (String pkg : packages) {
					if(clazz.getPackage().getName().contains(pkg)) {
						elected.add(clazz);
					}
				}
			}
		}
		
		return elected;
	}

@godart
Copy link
Contributor

godart commented Oct 9, 2019

True, meta annotations are not resolved by the getMethodsAnnotatedWith() call, so GetMapping, etc are not found.
Opened an upstream bug ronmamo/reflections#269

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

3 participants