From f3f5d0e78db8e582ac4f10d691c98d4d5870559a Mon Sep 17 00:00:00 2001 From: William Cheng Date: Mon, 10 Aug 2026 15:35:34 +0800 Subject: [PATCH] minor improvements to spring codegen --- .../codegen/languages/SpringCodegen.java | 49 +++++++++---------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java index 9906299b8f55..83e1fb20a863 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java @@ -128,7 +128,7 @@ public enum RequestMappingMode { controller("Generate the @RequestMapping annotation on the generated Api Controller Implementation."), none("Do not add a class level @RequestMapping annotation."); - private String description; + private final String description; RequestMappingMode(String description) { this.description = description; @@ -949,7 +949,6 @@ public void preprocessOpenAPI(OpenAPI openAPI) { if (openAPI.getPaths() != null) { for (final Map.Entry openAPIGetPathsEntry : openAPI.getPaths().entrySet()) { - final String pathname = openAPIGetPathsEntry.getKey(); final PathItem path = openAPIGetPathsEntry.getValue(); if (path.readOperations() != null) { for (final Operation operation : path.readOperations()) { @@ -960,9 +959,9 @@ public void preprocessOpenAPI(OpenAPI openAPI) { value.put("tag", escapeText(tag)); tags.add(value); } - if (operation.getTags().size() > 0) { + if (!operation.getTags().isEmpty()) { final String tag = operation.getTags().get(0); - operation.setTags(Arrays.asList(tag)); + operation.setTags(Collections.singletonList(tag)); } operation.addExtension("x-tags", tags); } @@ -1065,29 +1064,28 @@ private interface DataTypeAssigner { * fields in the model. */ private void doDataTypeAssignment(String returnType, DataTypeAssigner dataTypeAssigner) { - final String rt = returnType; - if (rt == null) { + if (returnType == null) { dataTypeAssigner.setReturnType("Void"); dataTypeAssigner.setIsVoid(true); - } else if (rt.startsWith("List") || rt.startsWith("java.util.List")) { - final int start = rt.indexOf("<"); - final int end = rt.lastIndexOf(">"); + } else if (returnType.startsWith("List") || returnType.startsWith("java.util.List")) { + final int start = returnType.indexOf("<"); + final int end = returnType.lastIndexOf(">"); if (start > 0 && end > 0) { - dataTypeAssigner.setReturnType(rt.substring(start + 1, end).trim()); + dataTypeAssigner.setReturnType(returnType.substring(start + 1, end).trim()); dataTypeAssigner.setReturnContainer("List"); } - } else if (rt.startsWith("Map") || rt.startsWith("java.util.Map")) { - final int start = rt.indexOf("<"); - final int end = rt.lastIndexOf(">"); + } else if (returnType.startsWith("Map") || returnType.startsWith("java.util.Map")) { + final int start = returnType.indexOf("<"); + final int end = returnType.lastIndexOf(">"); if (start > 0 && end > 0) { - dataTypeAssigner.setReturnType(rt.substring(start + 1, end).split(",", 2)[1].trim()); + dataTypeAssigner.setReturnType(returnType.substring(start + 1, end).split(",", 2)[1].trim()); dataTypeAssigner.setReturnContainer("Map"); } - } else if (rt.startsWith("Set") || rt.startsWith("java.util.Set")) { - final int start = rt.indexOf("<"); - final int end = rt.lastIndexOf(">"); + } else if (returnType.startsWith("Set") || returnType.startsWith("java.util.Set")) { + final int start = returnType.indexOf("<"); + final int end = returnType.lastIndexOf(">"); if (start > 0 && end > 0) { - dataTypeAssigner.setReturnType(rt.substring(start + 1, end).trim()); + dataTypeAssigner.setReturnType(returnType.substring(start + 1, end).trim()); dataTypeAssigner.setReturnContainer("Set"); } } @@ -1140,7 +1138,7 @@ public Map postProcessSupportingFileData(Map obj @Override public String toApiName(String name) { - if (name.length() == 0) { + if (name.isEmpty()) { return "DefaultApi"; } name = sanitizeName(name); @@ -1188,10 +1186,10 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert } // Add imports for Jackson - if (!Boolean.TRUE.equals(model.isEnum)) { + if (!model.isEnum) { model.imports.add("JsonProperty"); - if (Boolean.TRUE.equals(model.hasEnums)) { + if (model.hasEnums) { model.imports.add("JsonValue"); } } else { // enum class @@ -1243,7 +1241,7 @@ public CodegenModel fromModel(String name, Schema model) { } // Only add Nullable import for non-enum models that may have nullable fields - if (!Boolean.TRUE.equals(codegenModel.isEnum)) { + if (!codegenModel.isEnum) { addSpringNullableImport(codegenModel.imports); } @@ -1352,7 +1350,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation if (schemaTypes.containsKey("array")) { // we have a match with SSE pattern // double check potential conflicting, multiple specs - if (schemaTypes.keySet().size() > 1) { + if (schemaTypes.size() > 1) { throw new RuntimeException("only 1 response media type supported, when SSE is detected"); } // double check schema format @@ -1384,8 +1382,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation // Run through toModelName so that schemaMappings (e.g. User → com.example.MyUser) // are honored: the mapped name is used both in the type arg and for import resolution. String itemType = toModelName(detected.itemSchemaName); - String newBaseType = pagedModelClassName + "<" + itemType + ">"; - codegenOperation.returnType = newBaseType; + codegenOperation.returnType = pagedModelClassName + "<" + itemType + ">"; codegenOperation.returnBaseType = pagedModelClassName; // Clear any container flag — PagedModel is not itself a List/array codegenOperation.returnContainer = null; @@ -1432,7 +1429,7 @@ private Set reformatProvideArgsParams(Operation operation) { } } String newArg = String.join(" ", newArgs); - LOGGER.trace("new arg {} {}", newArg); + LOGGER.trace("new arg {}", newArg); formattedArgs.add(newArg); } }