|
94 | 94 | import static org.openapitools.codegen.CodegenConstants.*; |
95 | 95 | import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER; |
96 | 96 | import static org.openapitools.codegen.utils.DiscriminatorUtils.*; |
97 | | -import static org.openapitools.codegen.utils.EnumUtils.getEnumValues; |
| 97 | +import static org.openapitools.codegen.utils.EnumUtils.*; |
98 | 98 | import static org.openapitools.codegen.utils.OnceLogger.once; |
99 | 99 | import static org.openapitools.codegen.utils.StringUtils.*; |
100 | 100 |
|
@@ -6842,31 +6842,59 @@ protected List<Map<String, Object>> buildEnumVars(List<Object> values, String da |
6842 | 6842 | } |
6843 | 6843 |
|
6844 | 6844 | if (enumUnknownDefaultCase) { |
6845 | | - // If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response. |
6846 | | - // With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case. |
6847 | | - Map<String, Object> enumVar = new HashMap<>(); |
6848 | | - String enumName = enumUnknownDefaultCaseName; |
6849 | | - |
6850 | | - String enumValue = isDataTypeString(dataType) |
6851 | | - ? enumUnknownDefaultCaseName |
6852 | | - : // This is a dummy value that attempts to avoid collisions with previously specified cases. |
6853 | | - // Int.max / 192 |
6854 | | - // The number 192 that is used to calculate this random value, is the Swift Evolution proposal for frozen/non-frozen enums. |
6855 | | - // [SE-0192](https://github.com/apple/swift-evolution/blob/master/proposals/0192-non-exhaustive-enums.md) |
6856 | | - // Since this functionality was born in the Swift 5 generator and latter on broth to all generators |
6857 | | - // https://github.com/OpenAPITools/openapi-generator/pull/11013 |
6858 | | - String.valueOf(11184809); |
6859 | | - |
6860 | | - enumVar.put(ENUM_NAME, toEnumVarName(enumName, dataType)); |
6861 | | - enumVar.put(ENUM_VALUE, toEnumValue(enumValue, dataType)); |
6862 | | - enumVar.put(ENUM_IS_STRING, isDataTypeString(dataType)); |
6863 | | - // TODO: add isNumeric |
6864 | | - enumVars.add(enumVar); |
| 6845 | + injectEnumUnknownDefaultCase(enumVars, dataType); |
6865 | 6846 | } |
6866 | 6847 |
|
6867 | 6848 | return enumVars; |
6868 | 6849 | } |
6869 | 6850 |
|
| 6851 | + /** |
| 6852 | + * If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response. |
| 6853 | + * This adds a default case to the enum, {@link DefaultCodegen#enumUnknownDefaultCaseName}, that can be used as a fallback for unknown values. |
| 6854 | + * |
| 6855 | + * @param enumVars the enumVars |
| 6856 | + * @param dataType the data type of the enum parameter |
| 6857 | + */ |
| 6858 | + private void injectEnumUnknownDefaultCase(List<Map<String, Object>> enumVars, String dataType) { |
| 6859 | + Map<String, Object> enumVar = new HashMap<>(); |
| 6860 | + String enumName = enumUnknownDefaultCaseName; |
| 6861 | + |
| 6862 | + String enumValue = isDataTypeString(dataType) |
| 6863 | + ? enumUnknownDefaultCaseName |
| 6864 | + : // This is a dummy value that attempts to avoid collisions with previously specified cases. |
| 6865 | + // Int.max / 192 |
| 6866 | + // The number 192 that is used to calculate this random value, is the Swift Evolution proposal for frozen/non-frozen enums. |
| 6867 | + // [SE-0192](https://github.com/apple/swift-evolution/blob/master/proposals/0192-non-exhaustive-enums.md) |
| 6868 | + // Since this functionality was born in the Swift 5 generator and latter on broth to all generators |
| 6869 | + // https://github.com/OpenAPITools/openapi-generator/pull/11013 |
| 6870 | + String.valueOf(11184809); |
| 6871 | + |
| 6872 | + enumVar.put(ENUM_NAME, toEnumVarName(enumName, dataType)); |
| 6873 | + enumVar.put(ENUM_VALUE, toEnumValue(enumValue, dataType)); |
| 6874 | + enumVar.put(ENUM_IS_STRING, isDataTypeString(dataType)); |
| 6875 | + // TODO: add isNumeric |
| 6876 | + enumVars.add(enumVar); |
| 6877 | + } |
| 6878 | + |
| 6879 | + /** |
| 6880 | + * Removes any injected default enum value that was created with {@link DefaultCodegen#injectEnumUnknownDefaultCase} |
| 6881 | + * from an operation's non-body enum parameters. This can for example be of interest when generating client code |
| 6882 | + * where a fallback is superfluous for a value that is only sent and never received. |
| 6883 | + * |
| 6884 | + * @param operation operation to be processed |
| 6885 | + */ |
| 6886 | + protected void removeEnumUnknownDefaultCase(CodegenOperation operation) { |
| 6887 | + for (CodegenParameter param : operation.allParams) { |
| 6888 | + if (!param.isBodyParam && param.isEnum && hasEnumVars(param.allowableValues)) { |
| 6889 | + List<Map<String, Object>> enumVars = getEnumVars(param.allowableValues); |
| 6890 | + if (enumVars != null) { |
| 6891 | + String unknownName = toEnumVarName(enumUnknownDefaultCaseName, param.dataType); |
| 6892 | + enumVars.removeIf(ev -> unknownName.equals(ev.get(ENUM_NAME))); |
| 6893 | + } |
| 6894 | + } |
| 6895 | + } |
| 6896 | + } |
| 6897 | + |
6870 | 6898 | protected void postProcessEnumVars(List<Map<String, Object>> enumVars) { |
6871 | 6899 | Collections.reverse(enumVars); |
6872 | 6900 | enumVars.forEach(v -> { |
|
0 commit comments