-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathcreateExample.ts
More file actions
157 lines (134 loc) · 3.39 KB
/
createExample.ts
File metadata and controls
157 lines (134 loc) · 3.39 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
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
/* ============================================================================
* Copyright (c) Cloud Annotations
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* ========================================================================== */
import { SchemaObject } from "./types";
interface OASTypeToTypeMap {
string: string;
number: number;
integer: number;
boolean: boolean;
object: any;
array: any[];
null: string;
}
type Primitives = {
[OASType in keyof OASTypeToTypeMap]: {
[format: string]: (schema: SchemaObject) => OASTypeToTypeMap[OASType];
};
};
const primitives: Primitives = {
string: {
default: () => "string",
email: () => "user@example.com",
date: () => new Date().toISOString().substring(0, 10),
uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
hostname: () => "example.com",
ipv4: () => "198.51.100.42",
ipv6: () => "2001:0db8:5b96:0000:0000:426f:8e17:642a",
},
number: {
default: () => 0,
float: () => 0.0,
},
integer: {
default: () => 0,
},
boolean: {
default: (schema) =>
typeof schema.default === "boolean" ? schema.default : true,
},
null: {
default: () => "null",
},
object: {},
array: {},
};
export const sampleFromSchema = (schema: SchemaObject = {}): any => {
let { type, example, allOf, properties, items } = schema;
if (example !== undefined) {
return example;
}
if (allOf) {
// TODO: We are just assuming it will always be an object for now
let obj: SchemaObject = {
type: "object",
properties: {},
required: [], // NOTE: We shouldn't need to worry about required
};
for (let item of allOf) {
if (item.properties) {
obj.properties = {
...obj.properties,
...item.properties,
};
}
}
return sampleFromSchema(obj);
}
if (!type) {
if (properties) {
type = "object";
} else if (items) {
type = "array";
} else {
return;
}
}
if (type === "object") {
let obj: any = {};
for (let [name, prop] of Object.entries(properties ?? {})) {
if (prop.deprecated) {
continue;
}
obj[name] = sampleFromSchema(prop);
}
return obj;
}
if (type === "array") {
if (Array.isArray(items?.anyOf)) {
return items?.anyOf.map((item) => sampleFromSchema(item));
}
if (Array.isArray(items?.oneOf)) {
return items?.oneOf.map((item) => sampleFromSchema(item));
}
return [sampleFromSchema(items)];
}
if (schema.enum) {
if (schema.default) {
return schema.default;
}
return normalizeArray(schema.enum)[0];
}
return primitive(schema);
};
function primitive(schema: SchemaObject = {}): string {
let { type, format } = schema;
if (type instanceof Array) {
return type
.map((type) => primitive({ type, format }))
.reduce((acc, cur) => (acc ? `${acc} | ${cur}` : `${cur}`), "");
}
if (type === undefined || type === null) {
return "";
}
let fn = primitives[type]?.default;
if (fn === undefined) {
return "Unknown Type: " + type;
}
if (format !== undefined) {
fn = primitives[type][format] || fn;
}
if (fn) {
return fn(schema);
}
return "Unknown Type: " + schema.type;
}
function normalizeArray(arr: any) {
if (Array.isArray(arr)) {
return arr;
}
return [arr];
}