Skip to content

handle case where additionalRef is a ref (fixes #345) #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAPI2, OpenAPI3 } from "./types";
import { OpenAPI2, OpenAPI2Reference, OpenAPI2SchemaObject, OpenAPI3 } from "./types";

export function comment(text: string): string {
return `/**
Expand Down Expand Up @@ -121,3 +121,9 @@ export function unrefComponent(components: any, ref: string): any {
const [type, object] = ref.match(/(?<=\[")([^"]+)/g) as string[];
return components[type][object];
}

export function isOpenAPI2Reference(
additionalProperties: OpenAPI2SchemaObject | OpenAPI2Reference | boolean
): additionalProperties is OpenAPI2Reference {
return (additionalProperties as OpenAPI2Reference).$ref !== undefined;
}
14 changes: 11 additions & 3 deletions src/v2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import propertyMapper from "./property-mapper";
import { OpenAPI2, OpenAPI2SchemaObject, OpenAPI2Schemas, SwaggerToTSOptions } from "./types";
import { comment, nodeType, transformRef, tsArrayOf, tsIntersectionOf, tsUnionOf } from "./utils";
import { OpenAPI2, OpenAPI2Reference, OpenAPI2SchemaObject, OpenAPI2Schemas, SwaggerToTSOptions } from "./types";
import { comment, nodeType, transformRef, tsArrayOf, tsIntersectionOf, tsUnionOf, isOpenAPI2Reference } from "./utils";

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

//this functionality could perhaps be added to the nodeType function, but I don't want to mess with that code
function getAdditionalPropertiesType(additionalProperties: OpenAPI2SchemaObject | OpenAPI2Reference | boolean) {
if (isOpenAPI2Reference(additionalProperties)) {
return transformRef(additionalProperties.$ref, rawSchema ? "definitions/" : "");
}
return nodeType(additionalProperties);
}

// type conversions
function transform(node: OpenAPI2SchemaObject): string {
switch (nodeType(node)) {
Expand Down Expand Up @@ -67,7 +75,7 @@ export default function generateTypesV2(input: OpenAPI2 | OpenAPI2Schemas, optio

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

return tsIntersectionOf([
Expand Down
32 changes: 32 additions & 0 deletions tests/v2/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,38 @@ describe("transformation", () => {
);
});

it("additionalProperties 2 (issue #345)", () => {
const schema: OpenAPI2 = {
swagger: "2.0",
definitions: {
Messages: {
type: "object",
additionalProperties: {
$ref: "#/definitions/Message",
},
},
Message: {
type: "object",
properties: {
code: {
type: "integer",
},
text: {
type: "string",
},
},
},
},
};
expect(swaggerToTS(schema)).toBe(
format(`
export interface definitions {
Messages: { [key: string]: definitions["Message"] }
Message: { code?: number; text?: string }
}`)
);
});

it("allOf", () => {
const schema: OpenAPI2 = {
swagger: "2.0",
Expand Down