File size: 1,502 Bytes
322135d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const toOpenApi = require('@openapi-contrib/json-schema-to-openapi-schema');

const fs = require('fs');

let rawdata = fs.readFileSync('schema.json');

let schema = JSON.parse(rawdata);

// Generated result contains usage of "allOf" directive with a single class.
// It breaks client generator serves no function.
// This function should replace "allOf" directives with simple class usages
function replaceAllOf(schema) {
    if (Array.isArray(schema)) {
        var newSchema = [];
        for (var k in schema) {
            newSchema[k] = replaceAllOf(schema[k]);
        }
        return newSchema;
    }
    if (typeof schema === 'object' && schema !== null) {
        var newSchema = {};
        for (var k in schema) {
            if (k === 'allOf' && schema[k].length === 1) {
                newSchema = {...schema[k][0]};
                break
            } else {
                newSchema[k] = replaceAllOf(schema[k]);
            }
        }
        return newSchema;
    }
    return schema;
}



(async () => {
    var convertedSchema = await toOpenApi(schema);

    convertedSchema = replaceAllOf(convertedSchema);

    for (var modelName in convertedSchema['definitions']) {
        convertedSchema['definitions'][modelName]["$schema"] = schema["$schema"];
        convertedSchema['definitions'][modelName] = await toOpenApi(convertedSchema['definitions'][modelName]);
    }

    console.log(JSON.stringify({components: {schemas: convertedSchema['definitions']}}, null, 4));
})();