Skip to content

Commit

Permalink
feat: add allOf support (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tenischev authored Oct 24, 2020
1 parent 278a2bb commit f5fc389
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 36 deletions.
38 changes: 38 additions & 0 deletions filters/all.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
const filter = module.exports;
const _ = require('lodash');

function defineType(prop, propName) {
if (prop.type() === 'object') {
return _.upperFirst(_.camelCase(prop.uid()));
} else if (prop.type() === 'array') {
if (prop.items().format()) {
return toJavaType(prop.items().format()) + '[]';
} else {
return toJavaType(prop.items().type()) + '[]';
}
} else if (prop.enum() && (prop.type() === 'string' || prop.type() === 'integer')) {
return _.upperFirst(_.camelCase(propName)) + 'Enum';
} else if (prop.anyOf() || prop.oneOf()) {
let propType = 'OneOf';
let hasPrimitive = false;
[].concat(prop.anyOf(), prop.oneOf()).filter(obj != null).forEach(obj => {
hasPrimitive |= obj.type() !== 'object';
propType += _.upperFirst(_.camelCase(obj.uid()));
});
if (hasPrimitive) {
propType = 'Object';
}
return propType;
} else if (prop.allOf()) {
let propType = 'AllOf';
prop.allOf().forEach(obj => {
propType += _.upperFirst(_.camelCase(obj.uid()));
});
return propType;
} else {
if (prop.format()) {
return toJavaType(prop.format());
} else {
return toJavaType(prop.type());
}
}
}
filter.defineType = defineType;

function toJavaType(str){
switch(str) {
case 'integer':
Expand Down
30 changes: 28 additions & 2 deletions template/src/main/java/com/asyncapi/model/$$message$$.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package {{ params['userJavaPackage'] }}.model;

import java.util.Objects;
import javax.validation.Valid;

{% if message.description() or message.examples()%}/**{% for line in message.description() | splitByLines %}
* {{ line | safe}}{% endfor %}{% if message.examples() %}
Expand All @@ -24,10 +25,35 @@ public interface {{payloadName}} {

}
{%- endif %}
{%- else %}
{%- elif message.payload().allOf() %}
{%- set payloadName = 'AllOf' %}
{%- for obj in message.payload().allOf() %}
{%- set payloadName = payloadName + obj.uid() | camelCase | upperFirst %}
{%- endfor %}
public class {{payloadName}} {
{%- for obj in message.payload().allOf() %}
{%- set varName = obj.uid() | camelCase %}
{%- set className = obj.uid() | camelCase | upperFirst %}
{%- set propType = obj | defineType(obj.uid()) %}

{%- if obj.type() === 'array' %}
{%- set varName = obj.uid() | camelCase + 'Array' %}
{%- endif %}
private @Valid {{propType}} {{varName}};

public {{propType}} get{{className}}() {
return {{varName}};
}

public void set{{className}}({{propType}} {{varName}}) {
this.{{varName}} = {{varName}};
}
{%- endfor %}
}
{% else %}
{%- set payloadName = message.payload().uid() | camelCase | upperFirst %}
{%- endif %}
private {{payloadName}} payload;
private @Valid {{payloadName}} payload;

public {{payloadName}} getPayload() {
return payload;
Expand Down
69 changes: 35 additions & 34 deletions template/src/main/java/com/asyncapi/model/$$objectSchema$$.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public class {{schemaName | camelCase | upperFirst}} {
{%- endif %}
{%- elif prop.enum() and (prop.type() === 'string' or prop.type() === 'integer') %}
public enum {{propName | camelCase | upperFirst}}Enum {
{% for e in prop.enum() %}
{%- if prop.type() === 'string'%}
{% for e in prop.enum() %}
{%- if prop.type() === 'string'%}
{{e | upper | createEnum}}(String.valueOf("{{e}}")){% if not loop.last %},{% else %};{% endif %}
{%- else %}
{%- else %}
NUMBER_{{e}}({{e}}){% if not loop.last %},{% else %};{% endif %}
{%- endif %}
{% endfor %}
{%- endif %}
{% endfor %}
private {% if prop.type() === 'string'%}String{% else %}Integer{% endif %} value;

{{propName | camelCase | upperFirst}}Enum ({% if prop.type() === 'string'%}String{% else %}Integer{% endif %} v) {
Expand Down Expand Up @@ -79,6 +79,33 @@ public interface {{propType}} {
}
{%- endif %}
private @Valid {{propType}} {{propName | camelCase}};
{%- elif prop.allOf() %}
{%- set allName = 'AllOf' %}
{%- for obj in prop.allOf() %}
{%- set allName = allName + obj.uid() | camelCase | upperFirst %}
{%- endfor %}
public class {{allName}} {
{%- for obj in prop.allOf() %}
{%- set varName = obj.uid() | camelCase %}
{%- set className = obj.uid() | camelCase | upperFirst %}
{%- set propType = obj | defineType(obj.uid()) %}

{%- if obj.type() === 'array' %}
{%- set varName = obj.uid() | camelCase + 'Array' %}
{%- endif %}
private @Valid {{propType}} {{varName}};

public {{propType}} get{{className}}() {
return {{varName}};
}

public void set{{className}}({{propType}} {{varName}}) {
this.{{varName}} = {{varName}};
}
{%- endfor %}
}

private @Valid {{allName}} {{propName | camelCase}};
{%- else %}
{%- if prop.format() %}
private @Valid {{prop.format() | toJavaType}} {{propName | camelCase}};
Expand All @@ -91,36 +118,10 @@ public interface {{propType}} {
{% for propName, prop in schema.properties() %}
{%- set varName = propName | camelCase %}
{%- set className = propName | camelCase | upperFirst %}
{%- if prop.type() === 'object' %}
{%- set propType = prop.uid() | camelCase | upperFirst %}
{%- elif prop.type() === 'array' %}
{%- set propType = prop | defineType(propName) %}

{%- if prop.type() === 'array' %}
{%- set varName = propName | camelCase + 'Array' %}
{%- if prop.items().format() %}
{%- set propType = prop.items().format() | toJavaType + '[]' %}
{%- else %}
{%- set propType = prop.items().type() | toJavaType + '[]' %}
{%- endif %}
{%- elif prop.enum() and (prop.type() === 'string' or prop.type() === 'integer') %}
{%- set propType = (propName | camelCase | upperFirst) + 'Enum' %}
{%- elif prop.anyOf() or prop.oneOf() %}
{%- set propType = 'OneOf' %}{%- set hasPrimitive = false %}
{%- for obj in prop.anyOf() %}
{%- set hasPrimitive = hasPrimitive or obj.type() !== 'object' %}
{%- set propType = propType + obj.uid() | camelCase | upperFirst %}
{%- endfor %}
{%- for obj in prop.oneOf() %}
{%- set hasPrimitive = hasPrimitive or obj.type() !== 'object' %}
{%- set propType = propType + obj.uid() | camelCase | upperFirst %}
{%- endfor %}
{%- if hasPrimitive %}
{%- set propType = 'Object' %}
{%- endif %}
{%- else %}
{%- if prop.format() %}
{%- set propType = prop.format() | toJavaType %}
{%- else %}
{%- set propType = prop.type() | toJavaType %}
{%- endif %}
{%- endif %}

{% if prop.description() or prop.examples()%}/**{% for line in prop.description() | splitByLines %}
Expand Down

0 comments on commit f5fc389

Please sign in to comment.