Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Up 8127 #376

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions filters/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function createEnum(val){
};
filter.createEnum = createEnum;

function addBackSlashToPattern(val) {
function addBackSlashToPattern(val) {
let result = val.replace(/\\/g, "\\\\");
return result;
}
Expand Down Expand Up @@ -229,4 +229,68 @@ filter.toAmqpNeutral = toAmqpNeutral
function toAmqpKey(channelName, hasParameters, parameters) {
return toTopicString(channelName, hasParameters, parameters, true, "*")
}
filter.toAmqpKey = toAmqpKey
filter.toAmqpKey = toAmqpKey

const javaUnsafe = [
'abstract',
'assert',
'boolean',
'break',
'byte',
'case',
'catch',
'char',
'class',
'const',
'continue',
'default',
'double',
'do',
'else',
'enum',
'extends',
'false',
'final',
'finally',
'float',
'for',
'goto',
'if',
'implements',
'import',
'instanceof',
'int',
'interface',
'long',
'native',
'new',
'null',
'package',
'private',
'protected',
'public',
'return',
'short',
'static',
'strictfp',
'super',
'switch',
'synchronized',
'this',
'throw',
'throws',
'transient',
'true',
'try',
'void',
'volatile',
'while'
]

function javaSafe(val) {
if(javaUnsafe.includes(val)) {
return val + "_";
}
return val;
}
filter.javaSafe = javaSafe;
2 changes: 1 addition & 1 deletion hooks/02_removeNotRelevantParts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
if (!hasAmqp) {
// remove filers from template related only to amqp
}
if (!hasMqtt) {
if (!hasMqtt || generator.templateParams.onlyModels) {
// remove filers from template related only to mqtt
fs.unlinkSync(path.resolve(generator.targetDir, 'src/test/java/com/asyncapi/TestcontainerMqttTest.java'));
}
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@
"default": false,
"required": false
},
"onlyModels": {
"description": "Only generate the models for mqtt, not the config and test. This was added by Unifly because the generated config and test don't support custom converters",
"default": false,
"required": false
},
"maven": {
"description": "Generate pom.xml Maven build file instead of Gradle build",
"default": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
{{- amqpConfig(asyncapi, params) -}}
{%- endif -%}
{%- if asyncapi | isProtocol('mqtt') -%}
{%- if params.onlyModels !== true %}
{{- mqttConfig(asyncapi, params) -}}
{%- endif -%}
{%- endif -%}
{%- if (asyncapi | isProtocol('kafka')) or (asyncapi | isProtocol('kafka-secure')) -%}
{{- kafkaConfig(asyncapi, params) -}}
{%- endif -%}
28 changes: 19 additions & 9 deletions template/src/main/java/com/asyncapi/model/$$objectSchema$$.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class {{schemaName | camelCase | upperFirst}} {
private @Valid Map<String, {{prop.additionalProperties().type() | toJavaType | toClass}}> {{propName | camelCase}};
{%- endif %}
{%- elif prop.type() === 'object' %}
private @Valid {{prop.uid() | camelCase | upperFirst}} {{propName | camelCase}};
private @Valid {{prop.uid() | camelCase | upperFirst}} {{propName | camelCase | javaSafe}};
{%- elif prop.type() === 'array' %}
{%- if prop.items().type() === 'object' %}
private @Valid List<{{prop.items().uid() | camelCase | upperFirst}}> {{propName | camelCase}};
Expand Down Expand Up @@ -81,7 +81,7 @@ public String toString() {
}
}

private @Valid {{propName | camelCase | upperFirst}}Enum {{propName | camelCase}};
private @Valid {{propName | camelCase | upperFirst}}Enum {{propName | camelCase | javaSafe}};
{%- elif prop.anyOf() or prop.oneOf() %}
{%- set propType = 'OneOf' %}{%- set hasPrimitive = false %}
{%- for obj in prop.anyOf() %}
Expand All @@ -99,7 +99,7 @@ public interface {{propType}} {

}
{%- endif %}
private @Valid {{propType}} {{propName | camelCase}};
private @Valid {{propType}} {{propName | camelCase | javaSafe}};
{%- elif prop.allOf() %}
{%- set allName = 'AllOf' %}
{%- for obj in prop.allOf() %}
Expand All @@ -120,21 +120,26 @@ public class {{allName}} {
public void set{{className}}({{propType}} {{varName}}) {
this.{{varName}} = {{varName}};
}

public {{allName}} {{varName}}({{propType}} {{varName}}) {
this.{{varName}} = {{varName}};
return this;
}
{%- endfor %}
}

private @Valid {{allName}} {{propName | camelCase}};
private @Valid {{allName}} {{propName | camelCase | javaSafe}};
{%- else %}
{%- if prop.format() %}
private @Valid {{prop.format() | toJavaType(isRequired)}} {{propName | camelCase}};
private @Valid {{prop.format() | toJavaType(isRequired)}} {{propName | camelCase | javaSafe}};
{%- else %}
private @Valid {{prop.type() | toJavaType(isRequired)}} {{propName | camelCase}};
private @Valid {{prop.type() | toJavaType(isRequired)}} {{propName | camelCase | javaSafe}};
{%- endif %}
{%- endif %}
{% endfor %}

{% for propName, prop in schema.properties() %}
{%- set varName = propName | camelCase %}
{%- set varName = propName | camelCase | javaSafe %}
{%- set className = propName | camelCase | upperFirst %}
{%- set propType = prop | defineType(propName) | safe %}

Expand All @@ -144,7 +149,7 @@ public class {{allName}} {
*/{% endif %}
@JsonProperty("{{propName}}")
{%- if propName | isRequired(schema.required()) %}@NotNull{% endif %}
{%- if prop.minLength() or prop.maxLength() or prop.maxItems() or prop.minItems() %}@Size({% if prop.minLength() or prop.minItems() %}min = {{prop.minLength()}}{{prop.minItems()}}{% endif %}{% if prop.maxLength() or prop.maxItems() %}{% if prop.minLength() or prop.minItems() %},{% endif %}max = {{prop.maxLength()}}{{prop.maxItems()}}{% endif %}){% endif %}
{%- if prop.minLength() or prop.maxLength() or prop.maxItems() or prop.minItems() %}@Size({% if prop.minLength() or prop.minItems() %}min = {{prop.minLength()}}{{prop.minItems()}}{% endif %}{% if prop.maxLength() or prop.maxItems() %}{% if prop.minLength() or prop.minItems() %},{% endif %}max = {{prop.maxLength()}}{{prop.maxItems()}}{% endif %}){% endif %}
{%- if prop.pattern() %}@Pattern(regexp="{{prop.pattern() | addBackSlashToPattern}}"){% endif %}
{%- if prop.minimum() %}@Min({{prop.minimum()}}){% endif %}{% if prop.exclusiveMinimum() %}@Min({{prop.exclusiveMinimum() + 1}}){% endif %}
{%- if prop.maximum() %}@Max({{prop.maximum()}}){% endif %}{% if prop.exclusiveMaximum() %}@Max({{prop.exclusiveMaximum() + 1}}){% endif %}
Expand All @@ -155,6 +160,11 @@ public class {{allName}} {
public void set{{className}}({{propType}} {{varName}}) {
this.{{varName}} = {{varName}};
}

public {{schemaName | camelCase | upperFirst}} {{varName}}({{propType}} {{varName}}) {
this.{{varName}} = {{varName}};
return this;
}
{% endfor %}
{% if params.disableEqualsHashCode === 'false' %}@Override{% set hasProps = schema.properties() | length > 0 %}
public boolean equals(Object o) {
Expand Down Expand Up @@ -199,4 +209,4 @@ private String toIndentedString(Object o) {
}
return o.toString().replace("\n", "\n ");
}
}
}
Loading