Skip to content

Commit 47af2da

Browse files
committed
Create interfaces for additionalProperties objects
1 parent 12e7858 commit 47af2da

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
}
@@ -113,7 +114,7 @@ function parse(spec: Swagger2, namespace: string) {
113114
function buildNextInterface() {
114115
const nextObject = queue.pop();
115116
if (!nextObject) return; // Geez TypeScript it’s going to be OK
116-
const [ID, { allOf, properties, required }] = nextObject;
117+
const [ID, { allOf, properties, required, additionalProperties, type }] = nextObject;
117118

118119
let allProperties = properties || {};
119120
const includes: string[] = [];
@@ -132,7 +133,12 @@ function parse(spec: Swagger2, namespace: string) {
132133
}
133134

134135
// If nothing’s here, let’s skip this one.
135-
if (!Object.keys(allProperties).length) {
136+
if (
137+
!Object.keys(allProperties).length &&
138+
additionalProperties !== true &&
139+
type &&
140+
TYPES[type]
141+
) {
136142
return;
137143
}
138144
// Open interface
@@ -162,6 +168,17 @@ function parse(spec: Swagger2, namespace: string) {
162168
output.push(`${name}: ${type};`);
163169
});
164170

171+
if (additionalProperties) {
172+
if (<boolean>additionalProperties === true) {
173+
output.push(`[name: string]: ${ID}`);
174+
}
175+
176+
if ((<Swagger2Definition>additionalProperties).type) {
177+
const type = getType(<Swagger2Definition>additionalProperties, '');
178+
output.push(`[name: string]: ${type}`);
179+
}
180+
}
181+
165182
// Close interface
166183
output.push('}');
167184

@@ -181,7 +198,7 @@ function parse(spec: Swagger2, namespace: string) {
181198

182199
output.push('}'); // Close namespace
183200

184-
return prettier.format(output.join('\n'), { parser: 'typescript' });
201+
return prettier.format(output.join('\n'), { parser: 'typescript', singleQuote: true });
185202
}
186203

187204
export default parse;

0 commit comments

Comments
 (0)