Repository navigation
Expand file tree
/
Copy pathModels.utils.js
More file actions
57 lines (49 loc) · 1.7 KB
/
Copy pathModels.utils.js
File metadata and controls
57 lines (49 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { toJavaClassName } from './String.utils';
import { json } from 'generate-schema';
export function collateModelNames(asyncapi) {
return Object.keys(collateModels(asyncapi));
}
export function collateModels(asyncapi) {
const models = {};
for (const message of asyncapi.allMessages().all()) {
const id = message.id() || message.name();
models[toJavaClassName(id)] = message;
}
return models;
}
// The rest of the generator depends on a message object
// having a payload with properties. This is needed to
// be able to generate Java classes with attributes
// matching the expected properties.
//
// Some AsyncAPI documents don't include payload properties
// but provide a sample message instead. For these
// documents, we can attempt to derive a schema from
// the sample, and use that schema to generate a usable
// set of properties.
export function getMessagePayload(message) {
let payload = message.payload();
if (!payload) {
payload = {
required: () => { return false; }
};
}
if (!payload.properties || !payload.properties()) {
const generatedProperties = {};
const examples = message.examples().all();
if (examples && examples.length > 0) {
const example = examples[0];
const examplePayload = example.payload();
const jsonSchema = json('schema', examplePayload).properties;
Object.keys(jsonSchema).forEach((propertyName) => {
generatedProperties[propertyName] = {
type: () => { return jsonSchema[propertyName].type; },
format: () => { return; },
required: () => { return false; }
};
});
}
payload.properties = () => { return generatedProperties; };
}
return payload;
}