summaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-ui/src/app/model/swagger.ts
blob: 1886708654470d8c1d81fa51f4280f30d85857dd (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*******************************************************************************
 * Copyright (c) 2017 ZTE Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     ZTE - initial API and implementation and/or initial documentation
 *******************************************************************************/

export class SwaggerParameterClass {
    public description: string;
    public position: string;  // in path, query, header, body, form
    public name: string;
    public required: boolean;
    public type: string;
    public enum: any[];

    // if position is body
    public schema: SwaggerSchemaObject;

    constructor(options: any) {
        this.description = options.description;
        this.position = options.in;
        this.name = options.name;
        this.required = options.required;
        this.type = options.type;
        this.enum = options.enum;
        if (this.position === 'body') {
            this.schema = getSchemaObject(options.schema);
        }
    }
}

export class SwaggerHeader {
    public description: string;

    constructor(options: any) {
        this.description = options.description;
    }
}

export class SwaggerResponseClass {
    public description: string;
    public schema: SwaggerSchemaObject;
    public headers: any;

    constructor({description, schema, headers}) {
        this.description = description;

        if (schema) {
            this.schema = getSchemaObject(schema);
        }

        if (headers) {
            this.headers = {};
            for (const key in headers) {
                this.headers[key] = new SwaggerHeader(headers[key]);
            }
        }
    }
}

export class SwaggerMethod {
    public consumes: string[];
    public description: string;
    public operationId: string;
    public parameters: SwaggerParameterClass[];
    public produces: string[];
    public responses: any;
    public summary: string;
    public tags: string[];

    constructor({ consumes, description, operationId, parameters, produces, responses, summary, tags }) {
        this.consumes = consumes;
        this.description = description;
        this.operationId = operationId;
        this.parameters = parameters.map(param => new SwaggerParameterClass(param));
        this.produces = produces;
        this.responses = this.initResponses(responses);
        this.summary = summary;
        this.tags = tags;
    }

    private initResponses(responses: any): any {
        const responseObjs = {};
        for (const key in responses) {
            responseObjs[key] = new SwaggerResponseClass(responses[key]);
        }

        return responseObjs;
    }
}

export class SwaggerInfo {
    public title: string;
    public version: string;

    constructor({ title, version }) {
        this.title = title;
        this.version = version;
    }
}

export class SwaggerTag {
    public name: string;

    constructor({name}) {
        this.name = name;
    }
}

export class Swagger {
    public basePath: string;
    public definitions: any;
    public info: SwaggerInfo;
    public paths: any;
    public swagger: string;
    public tags: SwaggerTag[];

    constructor({basePath, definitions, info, paths, swagger, tags}) {
        this.basePath = basePath;
        this.definitions = this.initDefinitions(definitions);
        this.info = new SwaggerInfo(info);
        this.paths = this.initPaths(paths);
        this.swagger = swagger;
        if(tags) {
          this.tags = tags.map(tag => new SwaggerTag(tag));
        }
    }

    private initPaths(paths: any): any {
        const pathObjs = {};
        for (const key in paths) {
            pathObjs[key] = this.initPath(paths[key]);
        }
        return pathObjs;
    }

    private initPath(path: any): any {
        const pathObj = {};

        for (const key in path) {
            pathObj[key] = new SwaggerMethod(path[key]);
        }

        return pathObj;
    }

    private initDefinitions(definitions: any): any {
        const definitionObjs = {};
        for (const key in definitions) {
            definitionObjs[key] = getSchemaObject(definitions[key]);
        }
        return definitionObjs;
    }
}

export function getSchemaObject(definition: any) {
    if (definition.$ref) {
        return new SwaggerReferenceObject(definition);
    } else if (definition.type === 'array') {
        return new SwaggerModelArray(definition);
    } else if (definition.type === 'object') {
        if (definition.properties) {
            return new SwaggerModelSimple(definition);
        } else if (definition.additionalProperties) {
            return new SwaggerModelMap(definition);
        } else {
            return new SwaggerModel();
        }
    } else {
        return new SwaggerPrimitiveObject(definition);
    }
}

export class SwaggerSchemaObject {

}

export class SwaggerReferenceObject extends SwaggerSchemaObject {
    public $ref: string;

    constructor({ $ref }) {
        super();
        this.$ref = $ref;
    }
}

export class SwaggerPrimitiveObject extends SwaggerSchemaObject {
    public collectionFormat: string;
    public default: any;
    public enumValues: any[];
    public exclusiveMaximum: boolean;
    public exclusiveMinimum: boolean;
    public format: string;
    public maximum: number;
    public maxLength: number;
    public minimum: number;
    public minLength: number;
    public multipleOf: number;
    public pattern: string;
    public type: string;

    constructor(options: any) {
        super();
        this.collectionFormat = options.collectionFormat;
        this.default = options.default;
        this.enumValues = options.enum;
        this.exclusiveMaximum = options.exclusiveMaximum;
        this.exclusiveMinimum = options.exclusiveMinimum;
        this.format = options.format;
        this.maximum = options.maximum;
        this.maxLength = options.maxLength;
        this.minimum = options.minimum;
        this.minLength = options.minLength;
        this.multipleOf = options.multipleOf;
        this.pattern = options.pattern;
        this.type = options.type;
    }
}

export class SwaggerModel extends SwaggerSchemaObject {
    public type = 'object';
}

export class SwaggerModelSimple extends SwaggerModel {
    public properties = {};
    public required = [];

    constructor(options: any) {
        super();
        this.required = options.required;
        for (const key in options.properties) {
            this.properties[key] = getSchemaObject(options.properties[key]);
        }
    }
}

export class SwaggerModelMap extends SwaggerModel {
    public additionalProperties: SwaggerSchemaObject;

    constructor(options: any) {
        super();
        this.additionalProperties = getSchemaObject(options.additionalProperties);
    }
}

export class SwaggerModelArray extends SwaggerSchemaObject {
    public type = 'array';
    public items: SwaggerSchemaObject;

    constructor(options: any) {
        super();
        this.items = getSchemaObject(options.items);
    }
}