From e73eeb4fdf3d251ecb57b184ec477b9539d67314 Mon Sep 17 00:00:00 2001 From: lborupj Date: Fri, 9 Mar 2018 07:33:34 +0100 Subject: [PATCH] fix for stripping prefix on single enums (#7726) --- .../io/swagger/codegen/DefaultCodegen.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 68824a622a25..8fe2316d15aa 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -263,21 +263,25 @@ public Map postProcessModelsEnum(Map objs) { } /** - * Returns the common prefix of variables for enum naming + * Returns the common prefix of variables for enum naming if + * two or more variables are present * * @param vars List of variable names * @return the common prefix for naming */ public String findCommonPrefixOfVars(List vars) { - try { - String[] listStr = vars.toArray(new String[vars.size()]); - String prefix = StringUtils.getCommonPrefix(listStr); - // exclude trailing characters that should be part of a valid variable - // e.g. ["status-on", "status-off"] => "status-" (not "status-o") - return prefix.replaceAll("[a-zA-Z0-9]+\\z", ""); - } catch (ArrayStoreException e) { - return ""; + if (vars.size() > 1) { + try { + String[] listStr = vars.toArray(new String[vars.size()]); + String prefix = StringUtils.getCommonPrefix(listStr); + // exclude trailing characters that should be part of a valid variable + // e.g. ["status-on", "status-off"] => "status-" (not "status-o") + return prefix.replaceAll("[a-zA-Z0-9]+\\z", ""); + } catch (ArrayStoreException e) { + // do nothing, just return default value + } } + return ""; } /**