Skip to content

Commit f6d84ac

Browse files
committed
Create interfaces for additionalProperties objects
1 parent 9947306 commit f6d84ac

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/swagger-2.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface Swagger2Definition {
99
items?: Swagger2Definition;
1010
oneOf?: Swagger2Definition[];
1111
properties?: { [index: string]: Swagger2Definition };
12+
additionalProperties?: boolean | Swagger2Definition;
1213
required?: string[];
1314
type?: 'array' | 'boolean' | 'integer' | 'number' | 'object' | 'string';
1415
}
@@ -108,7 +109,7 @@ function parse(spec: Swagger2, namespace: string) {
108109
function buildNextInterface() {
109110
const nextObject = queue.pop();
110111
if (!nextObject) return; // Geez TypeScript it’s going to be OK
111-
const [ID, { allOf, properties, required }] = nextObject;
112+
const [ID, { allOf, properties, required, additionalProperties, type }] = nextObject;
112113

113114
let allProperties = properties || {};
114115
const includes: string[] = [];
@@ -127,7 +128,12 @@ function parse(spec: Swagger2, namespace: string) {
127128
}
128129

129130
// If nothing’s here, let’s skip this one.
130-
if (!Object.keys(allProperties).length) {
131+
if (
132+
!Object.keys(allProperties).length &&
133+
additionalProperties !== true &&
134+
type &&
135+
TYPES[type]
136+
) {
131137
return;
132138
}
133139
// Open interface
@@ -157,6 +163,17 @@ function parse(spec: Swagger2, namespace: string) {
157163
output.push(`${name}: ${type};`);
158164
});
159165

166+
if (additionalProperties) {
167+
if (<boolean>additionalProperties === true) {
168+
output.push(`[name: string]: ${ID}`);
169+
}
170+
171+
if ((<Swagger2Definition>additionalProperties).type) {
172+
const type = getType(<Swagger2Definition>additionalProperties, '');
173+
output.push(`[name: string]: ${type}`);
174+
}
175+
}
176+
160177
// Close interface
161178
output.push('}');
162179

@@ -176,7 +193,7 @@ function parse(spec: Swagger2, namespace: string) {
176193

177194
output.push('}'); // Close namespace
178195

179-
return prettier.format(output.join('\n'), { parser: 'typescript' });
196+
return prettier.format(output.join('\n'), { parser: 'typescript', singleQuote: true });
180197
}
181198

182199
export default parse;

0 commit comments

Comments
 (0)