Skip to content

Commit

Permalink
feat: Add anyOf and oneOf support (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tenischev authored Oct 9, 2020
1 parent e74ab0d commit ed169ba
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 43 deletions.
62 changes: 62 additions & 0 deletions hooks/04_processOneOfAnyOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const path = require('path');
const _ = require('lodash');
const replace = require('replace-in-file');

module.exports = {
'generate:after': generator => {
const asyncapi = generator.asyncapi;
const messages = asyncapi.allMessages();
const schemas = asyncapi.allSchemas();

let objectsRegistry = new Set();
let interfaces = new Map();

let objectProcessing = (className, parameters) => {
// process only if all Schemas in Any\OneOf are not primitive type
if (parameters && parameters.every(obj => obj.type() === 'object')) {
let interfaceName = _.upperFirst(_.camelCase(className)) + '.OneOf';
// At first collect all Schemas in Any\OneOf and build interface name
parameters.forEach(obj => {
objectsRegistry.add(obj.uid());
interfaceName += _.upperFirst(_.camelCase(obj.uid()));
});
// Assign interface name for each Schema in Any\OneOf
parameters.forEach(obj => {
if (interfaces.has(obj.uid())) {
if (!interfaces.get(obj.uid()).includes(interfaceName)) {
interfaces.set(obj.uid(), interfaces.get(obj.uid()) + ', ' + interfaceName);
}
} else {
interfaces.set(obj.uid(), interfaceName);
}
});
}
}
for (let [key, value] of messages) {
objectProcessing(value.uid(), [].concat(value.payload().anyOf(), value.payload().oneOf()).filter(obj => obj != null));
}
for (let [key, value] of schemas) {
if (value.type() === 'object') {
// To correctly resolve the name of the parent class, we have to go through the properties
Object.values(value.properties())
.map(prop => [].concat(prop.anyOf(), prop.oneOf())
.filter(obj => obj != null))
.forEach(array => objectProcessing(value.uid(), array));
}
}

for (let [key, value] of schemas) {
if (objectsRegistry.has(value.uid()) && value.type() === 'object') {
// Update definitions of model classes
const className = _.upperFirst(_.camelCase(value.uid()));
const reg = new RegExp('public class ' + className, 'g')
const options = {
files: path.resolve(generator.targetDir, `src/main/java/com/asyncapi/model/${key}.java`),
from: reg,
to: 'public class ' + className + ' implements ' + interfaces.get(value.uid()),
};
replace.sync(options);
}
}
}
};
File renamed without changes.
File renamed without changes.
112 changes: 70 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@asyncapi/generator-hooks": "^0.1.0",
"fs-extra": "^9.0.0",
"lodash": "^4.17.15",
"replace-in-file": "^6.1.0",
"tmp": "^0.2.1"
},
"devDependencies": {
Expand Down
21 changes: 20 additions & 1 deletion template/src/main/java/com/asyncapi/model/$$message$$.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,26 @@
* Examples: {{message.examples() | examplesToString | safe}}{% endif %}
*/{% endif %}
public class {{messageName | camelCase | upperFirst}} {
{% set payloadName = message.payload().uid() | camelCase | upperFirst %}
{%- if message.payload().anyOf() or message.payload().oneOf() %}
{%- set payloadName = 'OneOf' %}{%- set hasPrimitive = false %}
{%- for obj in message.payload().anyOf() %}
{%- set hasPrimitive = hasPrimitive or obj.type() !== 'object' %}
{%- set payloadName = payloadName + obj.uid() | camelCase | upperFirst %}
{%- endfor %}
{%- for obj in message.payload().oneOf() %}
{%- set hasPrimitive = hasPrimitive or obj.type() !== 'object' %}
{%- set payloadName = payloadName + obj.uid() | camelCase | upperFirst %}
{%- endfor %}
{%- if hasPrimitive %}
{%- set payloadName = 'Object' %}
{%- else %}
public interface {{payloadName}} {

}
{%- endif %}
{%- else %}
{%- set payloadName = message.payload().uid() | camelCase | upperFirst %}
{%- endif %}
private {{payloadName}} payload;

public {{payloadName}} getPayload() {
Expand Down
Loading

0 comments on commit ed169ba

Please sign in to comment.