|
| 1 | +package cloud.stackit.codegen; |
| 2 | + |
| 3 | +import org.openapitools.codegen.CodegenProperty; |
| 4 | +import org.openapitools.codegen.languages.GoClientCodegen; |
| 5 | + |
| 6 | +import org.openapitools.codegen.CodegenParameter; |
| 7 | + |
| 8 | +import java.util.Set; |
| 9 | +import io.swagger.v3.oas.models.media.Schema; |
| 10 | +import io.swagger.v3.oas.models.parameters.Parameter; |
| 11 | + |
| 12 | +public class CustomRegionGenerator extends GoClientCodegen { |
| 13 | + |
| 14 | + @Override |
| 15 | + public String getName() { |
| 16 | + // This is the name you will pass to the -g flag |
| 17 | + return "cloud.stackit.codegen.CustomRegionGenerator"; |
| 18 | + } |
| 19 | + |
| 20 | + public CustomRegionGenerator() { |
| 21 | + super(); |
| 22 | + System.out.println("=== CUSTOM GO CLIENT GENERATOR INITIALIZED ==="); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public CodegenProperty fromProperty(String name, Schema p, boolean required) { |
| 27 | + CodegenProperty property = super.fromProperty(name, p, required); |
| 28 | + |
| 29 | + if (isRegionField(property.name)) { |
| 30 | + property.dataType = "string"; |
| 31 | + property.datatypeWithEnum = "string"; |
| 32 | + property.baseType = "string"; |
| 33 | + |
| 34 | + // Force template engine to treat this as a string |
| 35 | + property.isString = true; |
| 36 | + property.isInteger = false; |
| 37 | + property.isLong = false; |
| 38 | + property.isNumber = false; |
| 39 | + property.isNumeric = false; |
| 40 | + } |
| 41 | + return property; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Intercepts operation parameters (query, path, header, body). |
| 46 | + */ |
| 47 | + @Override |
| 48 | + public CodegenParameter fromParameter(Parameter param, Set<String> imports) { |
| 49 | + CodegenParameter parameter = super.fromParameter(param, imports); |
| 50 | + |
| 51 | + if (isRegionField(parameter.paramName)) { |
| 52 | + parameter.dataType = "string"; |
| 53 | + |
| 54 | + // Force template engine to treat this as a string |
| 55 | + parameter.isString = true; |
| 56 | + parameter.isInteger = false; |
| 57 | + parameter.isLong = false; |
| 58 | + parameter.isNumber = false; |
| 59 | + // If it was previously an enum or another complex type, clear it |
| 60 | + parameter.isEnum = false; |
| 61 | + } |
| 62 | + return parameter; |
| 63 | + } |
| 64 | + |
| 65 | + private boolean isRegionField(String name) { |
| 66 | + if (name == null) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + return name.equalsIgnoreCase("region") || name.equalsIgnoreCase("regionId"); |
| 70 | + } |
| 71 | +} |
0 commit comments