1 /*******************************************************************************
2 * Copyright (c) 2017 ZTE Corporation.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * ZTE - initial API and implementation and/or initial documentation
11 *******************************************************************************/
13 export class SwaggerParameter {
14 public description: string;
15 public position: string; // in path, query, header, body, form
17 public required: boolean;
20 // if position is body
21 public schema: SwaggerSchemaObject;
23 constructor(options: any) {
24 this.description = options.description;
25 this.position = options.in;
26 this.name = options.name;
27 this.required = options.required;
28 this.type = options.type;
29 if (this.position === 'body') {
30 this.schema = getSchemaObject(options.schema);
35 export class SwaggerHeader {
36 public description: string;
38 constructor(options: any) {
39 this.description = options.description;
43 export class SwaggerResponse {
44 public description: string;
45 public schema: SwaggerSchemaObject;
48 constructor({description, schema, headers}) {
49 this.description = description;
52 this.schema = getSchemaObject(schema);
57 for (const key in headers) {
58 this.headers[key] = new SwaggerHeader(headers[key]);
64 export class SwaggerMethod {
65 public consumes: string[];
66 public description: string;
67 public operationId: string;
68 public parameters: SwaggerParameter[];
69 public produces: string[];
70 public responses: any;
71 public summary: string;
72 public tags: string[];
74 constructor({ consumes, description, operationId, parameters, produces, responses, summary, tags }) {
75 this.consumes = consumes;
76 this.description = description;
77 this.operationId = operationId;
78 this.parameters = parameters.map(param => new SwaggerParameter(param));
79 this.produces = produces;
80 this.responses = this.initResponses(responses);
81 this.summary = summary;
85 private initResponses(responses: any): any {
86 const responseObjs = {};
87 for (const key in responses) {
88 responseObjs[key] = new SwaggerResponse(responses[key]);
95 export class SwaggerInfo {
97 public version: string;
99 constructor({ title, version }) {
101 this.version = version;
105 export class SwaggerTag {
108 constructor({name}) {
113 export class Swagger {
114 public basePath: string;
115 public definitions: any;
116 public info: SwaggerInfo;
118 public swagger: string;
119 public tags: SwaggerTag[];
121 constructor({basePath, definitions, info, paths, swagger, tags}) {
122 this.basePath = basePath;
123 this.definitions = this.initDefinitions(definitions);
124 this.info = new SwaggerInfo(info);
125 this.paths = this.initPaths(paths);
126 this.swagger = swagger;
127 this.tags = tags.map(tag => new SwaggerTag(tag));
130 private initPaths(paths: any): any {
132 for (const key in paths) {
133 pathObjs[key] = this.initPath(paths[key]);
138 private initPath(path: any): any {
141 for (const key in path) {
142 pathObj[key] = new SwaggerMethod(path[key]);
148 private initDefinitions(definitions: any): any {
149 const definitionObjs = {};
150 for (const key in definitions) {
151 definitionObjs[key] = getSchemaObject(definitions[key]);
153 return definitionObjs;
157 export function getSchemaObject(definition: any) {
158 if (definition.$ref) {
159 return new SwaggerReferenceObject(definition);
160 } else if (definition.type === 'array') {
161 return new SwaggerModelArray(definition);
162 } else if (definition.type === 'object') {
163 if (definition.properties) {
164 return new SwaggerModelSimple(definition);
165 } else if (definition.additionalProperties) {
166 return new SwaggerModelMap(definition);
168 return new SwaggerModel();
171 return new SwaggerPrimitiveObject(definition);
175 export class SwaggerSchemaObject {
179 export class SwaggerReferenceObject extends SwaggerSchemaObject {
182 constructor({ $ref }) {
188 export class SwaggerPrimitiveObject extends SwaggerSchemaObject {
189 public collectionFormat: string;
190 public defaultValue: any;
191 public enumValues: any[];
192 public exclusiveMaximum: boolean;
193 public exclusiveMinimum: boolean;
194 public format: string;
195 public maximum: number;
196 public maxLength: number;
197 public minimum: number;
198 public minLength: number;
199 public multipleOf: number;
200 public pattern: string;
203 constructor(options: any) {
205 this.collectionFormat = options.collectionFormat;
206 this.defaultValue = options.default;
207 this.enumValues = options.enum;
208 this.exclusiveMaximum = options.exclusiveMaximum;
209 this.exclusiveMinimum = options.exclusiveMinimum;
210 this.format = options.format;
211 this.maximum = options.maximum;
212 this.maxLength = options.maxLength;
213 this.minimum = options.minimum;
214 this.minLength = options.minLength;
215 this.multipleOf = options.multipleOf;
216 this.pattern = options.pattern;
217 this.type = options.type;
221 export class SwaggerModel extends SwaggerSchemaObject {
222 public type = 'object';
225 export class SwaggerModelSimple extends SwaggerModel {
226 public properties = {};
227 public required = [];
229 constructor(options: any) {
231 this.required = options.required;
232 for (const key in options.properties) {
233 this.properties[key] = getSchemaObject(options.properties[key]);
238 export class SwaggerModelMap extends SwaggerModel {
239 public additionalProperties: SwaggerSchemaObject;
241 constructor(options: any) {
243 this.additionalProperties = getSchemaObject(options.additionalProperties);
247 export class SwaggerModelArray extends SwaggerSchemaObject {
248 public type = 'array';
249 public items: SwaggerSchemaObject;
251 constructor(options: any) {
253 this.items = getSchemaObject(options.items);