I am currently trying to migrate from Springfox to Springdoc.
Most of my endpoints contain a @PathVariable Instance
. Obviously I am only passing in the ID of the instance and Spring would automatically resolve the object for me.
Here is an example:
@RestController @RequestMapping(value = "/api/{instance}/role") public class RoleController { @GetMapping() public ResponseEntity<?> getRoles(@PathVariable Instance instance) { return ResponseEntity.ok().build(); } }
Called as /api/myInstance/role
.
Springfox would handle this correctly for me, only generating {instance}
as a String
mapping to the ID of Instance
. However, Springdoc expects the entire object to be passed as a path param.
I did try this so far, however it didn’t seem to have any effect:
static { SpringDocUtils.getConfig().replaceParameterObjectWithClass(Instance.class, String.class); }
If possible, I’d like to avoid annotating hundreds of endpoints with the same annotation and solve this on a global level.
Advertisement
Answer
You can define your own ParameterCustomizer
:
@Component public class InstanceParameterCustomizer implements org.springdoc.core.customizers.ParameterCustomizer { @Override public Parameter customize(Parameter parameterModel, MethodParameter methodParameter) { if (Instance.class.equals(methodParameter.getParameterType()) && methodParameter.getParameterAnnotation(PathVariable.class) != null) { parameterModel.setName("id"); // if you need to change the name ... parameterModel.setSchema(new StringSchema()); } return parameterModel; } }