diff --git a/filters/all.js b/filters/all.js index aa6fe7828..d1431ab4f 100644 --- a/filters/all.js +++ b/filters/all.js @@ -89,3 +89,15 @@ function schemeExists(collection, scheme){ return _.some(collection, {'scheme': scheme}); }; filter.schemeExists = schemeExists; + +function createEnum(val){ + let result; + let withoutNonWordChars = val.replace(/[^A-Z^a-z^0-9]/g, "_"); + if ((new RegExp('^[^A-Z^a-z]', 'i')).test(withoutNonWordChars)) { + result = '_' + withoutNonWordChars; + } else { + result = withoutNonWordChars; + } + return result; +}; +filter.createEnum = createEnum; diff --git a/template/src/main/java/com/asyncapi/model/$$schema$$.java b/template/src/main/java/com/asyncapi/model/$$schema$$.java index 5bfb79027..c724b97fe 100644 --- a/template/src/main/java/com/asyncapi/model/$$schema$$.java +++ b/template/src/main/java/com/asyncapi/model/$$schema$$.java @@ -28,7 +28,7 @@ public class {{schemaName | camelCase | upperFirst}} { public enum {{propName | camelCase | upperFirst}}Enum { {% for e in prop.enum() %} {%- if prop.type() === 'string'%} - {{e | upper | replace(' ', '_')}}(String.valueOf("{{e}}")){% if not loop.last %},{% else %};{% endif %} + {{e | upper | createEnum}}(String.valueOf("{{e}}")){% if not loop.last %},{% else %};{% endif %} {%- else %} NUMBER_{{e}}({{e}}){% if not loop.last %},{% else %};{% endif %} {%- endif %}