Skip to content

Commit

Permalink
feat: Updated behaviour of AdditionalProperties when owning OptionalP…
Browse files Browse the repository at this point in the history
…ropery (#72)
  • Loading branch information
Himenon authored Mar 8, 2022
1 parent e825cca commit 91a94d4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/internal/OpenApiTools/toTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ export const convert: Convert = (
return nullable(factory, typeNode, schema.nullable);
}
if (option && option.parent) {
Logger.info("Parent Schema:");
Logger.info(JSON.stringify(option.parent));
const message = ["Schema Type is not found and is converted to the type any. The parent Schema is as follows.", "", JSON.stringify(option.parent), ""].join("\n");
Logger.info(message);
}
const typeNode = factory.TypeNode.create({
type: "any",
Expand Down Expand Up @@ -253,6 +253,7 @@ export const convert: Convert = (
value: [],
});
}

const value: ts.PropertySignature[] = Object.entries(schema.properties || {}).map(([name, jsonSchema]) => {
return factory.PropertySignature.create({
name: converterContext.escapePropertySignatureName(name),
Expand All @@ -268,6 +269,21 @@ export const convert: Convert = (
parent: schema.properties,
}),
});

const hasOptionalProperty = Object.keys(schema.properties || {}).some(key => !required.includes(key));
if (hasOptionalProperty) {
const objectTypeNode = factory.TypeNode.create({
type: schema.type,
value: value,
});
const additionalObjectTypeNode = factory.TypeNode.create({
type: schema.type,
value: [additionalProperties],
});
return factory.IntersectionTypeNode.create({
typeNodes: [objectTypeNode, additionalObjectTypeNode],
});
}
return factory.TypeNode.create({
type: schema.type,
value: [...value, additionalProperties],
Expand Down
8 changes: 8 additions & 0 deletions test/__tests__/__snapshots__/typedef-only-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ export namespace Schemas {
}
export type InferStringEnum = \\"a\\" | \\"b\\" | \\"c\\";
export type InferAnyNullable = null;
export interface OptionalPropertiesAndAdditionalProperties {
body?: {
key?: string;
description?: string;
} & {
[key: string]: string;
};
}
}
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,14 @@ export namespace Schemas {
}
export type InferStringEnum = \\"a\\" | \\"b\\" | \\"c\\";
export type InferAnyNullable = null;
export interface OptionalPropertiesAndAdditionalProperties {
body?: {
key?: string;
description?: string;
} & {
[key: string]: string;
};
}
}
export type HttpMethod = \\"GET\\" | \\"PUT\\" | \\"POST\\" | \\"DELETE\\" | \\"OPTIONS\\" | \\"HEAD\\" | \\"PATCH\\" | \\"TRACE\\";
export interface ObjectLike {
Expand Down
12 changes: 12 additions & 0 deletions test/infer.domain/index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ components:
enum: [a, b, c]
InferAnyNullable:
nullable: true
OptionalPropertiesAndAdditionalProperties:
type: object
properties:
body:
type: object
properties:
key:
type: string
description:
type: string
additionalProperties:
type: string

0 comments on commit 91a94d4

Please sign in to comment.