-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add anyOf and oneOf support (#86)
- Loading branch information
Showing
7 changed files
with
184 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.