How to mask any parameter in auto generated swagger java class ?
If manual getter setter class we can edit toString method but in autogenerated class it will generate swagger pojo class on each build so how to modify toString method which is autogenerated by swagger ?
Below is the code
api.yml ( which is generating POJO class through swagger)
InsuredDTO:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
nric:
type: string
toString Method inside auto generate POJO class
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class InsuredDTO {\n");
sb.append(" id: ").append(toIndentedString(id)).append("\n");
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append(" nric: ").append(toIndentedString(nric)).append("\n");
sb.append("}");
return sb.toString();
}
Adding some logs inside LoggingAspect class
log.debug("Enter: {}.{}() with argument[s] = {}", joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()));
In this log joinPoint.getArgs()
is having complete toString data with Id name and nric, here I want to display masked nric instead of direct nric value.
If it is not auto generated class then we can add something like below in toString method.
sb.append("nric: ").append("********").append("\n");
But in this case of auto generated class is there any way to mask any property only for logging purpose ?
I have tried with flag in yml file as below
properties:
id:
type: integer
format: int64
nric:
type: string
format: password
But this one also not formating the property in expected way.
I add a similar requirement on an old project, I used an extension field (see OpenAPI 3.0 Spec , end of file) like x-nolog
and a custom code generator :
Example of schema :
User:
type: object
required:
- name
- password
x-nolog:
- password
properties:
name:
type: string
password:
type: string
To create a custom code generator, you can see :
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.