From 034e9b46ef8138908575e3c7554723cf6b4f5903 Mon Sep 17 00:00:00 2001 From: dilanSachi Date: Fri, 4 Nov 2022 23:19:11 +0530 Subject: [PATCH] Add custom field value resolver --- .../ballerinalang/openapi/CodeGenerator.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/misc/openapi-ballerina/modules/openapi-to-ballerina-generator/src/main/java/org/ballerinalang/openapi/CodeGenerator.java b/misc/openapi-ballerina/modules/openapi-to-ballerina-generator/src/main/java/org/ballerinalang/openapi/CodeGenerator.java index 73321a5b84e1..f47524702c59 100644 --- a/misc/openapi-ballerina/modules/openapi-to-ballerina-generator/src/main/java/org/ballerinalang/openapi/CodeGenerator.java +++ b/misc/openapi-ballerina/modules/openapi-to-ballerina-generator/src/main/java/org/ballerinalang/openapi/CodeGenerator.java @@ -42,6 +42,7 @@ import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; +import java.lang.reflect.AccessibleObject; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -50,8 +51,10 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import static org.ballerinalang.openapi.model.GenSrcFile.GenFileType; import static org.ballerinalang.openapi.utils.GeneratorConstants.GenType.GEN_CLIENT; @@ -414,8 +417,30 @@ private List generateBallerinaService(BallerinaOpenApiType api) thro */ private String getContent(BallerinaOpenApiType object, String templateDir, String templateName) throws IOException { Template template = compileTemplate(templateDir, templateName); + FieldValueResolver fieldValueResolver = new FieldValueResolver() { + + @Override + protected Set members( + Class clazz) { + Set members = super.members(clazz); + return (Set) members.stream() + .filter(fw -> isValidField((FieldWrapper) fw)) + .collect(Collectors.toSet()); + } + + boolean isValidField( + FieldWrapper fw) { + if (fw instanceof AccessibleObject) { + if (isUseSetAccessible(fw)) { + return true; + } + return false; + } + return true; + } + }; Context context = Context.newBuilder(object) - .resolver(MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, FieldValueResolver.INSTANCE) + .resolver(MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, fieldValueResolver) .build(); return template.apply(context); }