Skip to content

Commit 91a94d4

Browse files
authored
feat: Updated behaviour of AdditionalProperties when owning OptionalPropery (#72)
1 parent e825cca commit 91a94d4

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

Diff for: src/internal/OpenApiTools/toTypeNode.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ export const convert: Convert = (
169169
return nullable(factory, typeNode, schema.nullable);
170170
}
171171
if (option && option.parent) {
172-
Logger.info("Parent Schema:");
173-
Logger.info(JSON.stringify(option.parent));
172+
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");
173+
Logger.info(message);
174174
}
175175
const typeNode = factory.TypeNode.create({
176176
type: "any",
@@ -253,6 +253,7 @@ export const convert: Convert = (
253253
value: [],
254254
});
255255
}
256+
256257
const value: ts.PropertySignature[] = Object.entries(schema.properties || {}).map(([name, jsonSchema]) => {
257258
return factory.PropertySignature.create({
258259
name: converterContext.escapePropertySignatureName(name),
@@ -268,6 +269,21 @@ export const convert: Convert = (
268269
parent: schema.properties,
269270
}),
270271
});
272+
273+
const hasOptionalProperty = Object.keys(schema.properties || {}).some(key => !required.includes(key));
274+
if (hasOptionalProperty) {
275+
const objectTypeNode = factory.TypeNode.create({
276+
type: schema.type,
277+
value: value,
278+
});
279+
const additionalObjectTypeNode = factory.TypeNode.create({
280+
type: schema.type,
281+
value: [additionalProperties],
282+
});
283+
return factory.IntersectionTypeNode.create({
284+
typeNodes: [objectTypeNode, additionalObjectTypeNode],
285+
});
286+
}
271287
return factory.TypeNode.create({
272288
type: schema.type,
273289
value: [...value, additionalProperties],

Diff for: test/__tests__/__snapshots__/typedef-only-test.ts.snap

+8
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,14 @@ export namespace Schemas {
314314
}
315315
export type InferStringEnum = \\"a\\" | \\"b\\" | \\"c\\";
316316
export type InferAnyNullable = null;
317+
export interface OptionalPropertiesAndAdditionalProperties {
318+
body?: {
319+
key?: string;
320+
description?: string;
321+
} & {
322+
[key: string]: string;
323+
};
324+
}
317325
}
318326
"
319327
`;

Diff for: test/__tests__/__snapshots__/typedef-with-template-test.ts.snap

+8
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,14 @@ export namespace Schemas {
10411041
}
10421042
export type InferStringEnum = \\"a\\" | \\"b\\" | \\"c\\";
10431043
export type InferAnyNullable = null;
1044+
export interface OptionalPropertiesAndAdditionalProperties {
1045+
body?: {
1046+
key?: string;
1047+
description?: string;
1048+
} & {
1049+
[key: string]: string;
1050+
};
1051+
}
10441052
}
10451053
export type HttpMethod = \\"GET\\" | \\"PUT\\" | \\"POST\\" | \\"DELETE\\" | \\"OPTIONS\\" | \\"HEAD\\" | \\"PATCH\\" | \\"TRACE\\";
10461054
export interface ObjectLike {

Diff for: test/infer.domain/index.yml

+12
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,15 @@ components:
3737
enum: [a, b, c]
3838
InferAnyNullable:
3939
nullable: true
40+
OptionalPropertiesAndAdditionalProperties:
41+
type: object
42+
properties:
43+
body:
44+
type: object
45+
properties:
46+
key:
47+
type: string
48+
description:
49+
type: string
50+
additionalProperties:
51+
type: string

0 commit comments

Comments
 (0)