Skip to content

Commit 89fc1d0

Browse files
committed
handle case where additionalRef is a ref (fixes #345)
1 parent 135a93b commit 89fc1d0

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

src/types/OpenAPI2.ts

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export type OpenAPI2Type =
3232

3333
export type OpenAPI2Reference = { $ref: string };
3434

35+
export function isOpenAPI2Reference(
36+
additionalProperties: OpenAPI2SchemaObject | OpenAPI2Reference | boolean
37+
): additionalProperties is OpenAPI2Reference {
38+
return (additionalProperties as OpenAPI2Reference).$ref !== undefined;
39+
}
40+
3541
export interface OpenAPI2SchemaObject {
3642
additionalProperties?: OpenAPI2SchemaObject | OpenAPI2Reference | boolean;
3743
allOf?: OpenAPI2SchemaObject[];

src/v2.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import propertyMapper from "./property-mapper";
2-
import { OpenAPI2, OpenAPI2SchemaObject, OpenAPI2Schemas, SwaggerToTSOptions } from "./types";
2+
import {
3+
OpenAPI2,
4+
OpenAPI2Reference,
5+
OpenAPI2SchemaObject,
6+
OpenAPI2Schemas,
7+
SwaggerToTSOptions,
8+
isOpenAPI2Reference,
9+
} from "./types";
310
import { comment, nodeType, transformRef, tsArrayOf, tsIntersectionOf, tsUnionOf } from "./utils";
411

512
export const PRIMITIVES: { [key: string]: "boolean" | "string" | "number" } = {
@@ -40,6 +47,14 @@ export default function generateTypesV2(input: OpenAPI2 | OpenAPI2Schemas, optio
4047
// propertyMapper
4148
const propertyMapped = options ? propertyMapper(definitions, options.propertyMapper) : definitions;
4249

50+
//this functionality could perhaps be added to the nodeType function, but I don't want to mess with that code
51+
function getAdditionalPropertiesType(additionalProperties: OpenAPI2SchemaObject | OpenAPI2Reference | boolean) {
52+
if (isOpenAPI2Reference(additionalProperties)) {
53+
return transformRef(additionalProperties.$ref, rawSchema ? "definitions/" : "");
54+
}
55+
return nodeType(additionalProperties);
56+
}
57+
4358
// type conversions
4459
function transform(node: OpenAPI2SchemaObject): string {
4560
switch (nodeType(node)) {
@@ -67,7 +82,7 @@ export default function generateTypesV2(input: OpenAPI2 | OpenAPI2Schemas, optio
6782

6883
// if additional properties, add to end of properties
6984
if (node.additionalProperties) {
70-
properties += `[key: string]: ${nodeType(node.additionalProperties) || "any"};\n`;
85+
properties += `[key: string]: ${getAdditionalPropertiesType(node.additionalProperties) || "any"};\n`;
7186
}
7287

7388
return tsIntersectionOf([

tests/v2/index.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,38 @@ describe("transformation", () => {
255255
);
256256
});
257257

258+
it("additionalProperties 2 (issue #345)", () => {
259+
const schema: OpenAPI2 = {
260+
swagger: "2.0",
261+
definitions: {
262+
Messages: {
263+
type: "object",
264+
additionalProperties: {
265+
$ref: "#/definitions/Message",
266+
},
267+
},
268+
Message: {
269+
type: "object",
270+
properties: {
271+
code: {
272+
type: "integer",
273+
},
274+
text: {
275+
type: "string",
276+
},
277+
},
278+
},
279+
},
280+
};
281+
expect(swaggerToTS(schema)).toBe(
282+
format(`
283+
export interface definitions {
284+
Messages: { [key: string]: definitions["Message"] }
285+
Message: { code?: number; text?: string }
286+
}`)
287+
);
288+
});
289+
258290
it("allOf", () => {
259291
const schema: OpenAPI2 = {
260292
swagger: "2.0",

0 commit comments

Comments
 (0)