Skip to content

Commit 43b9919

Browse files
authored
Merge pull request #3646 from codercms/fix/transformers-additional-properties
fix(transformers): prevent additionalProperties transforms from affecting declared object properties
2 parents dd7f255 + 7e4050b commit 43b9919

5 files changed

Lines changed: 39 additions & 6 deletions

File tree

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/transformers-additional-properties/transformers.gen.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ const fooSchemaResponseTransformer = (data: any) => {
99

1010
const barSchemaResponseTransformer = (data: any) => {
1111
data.bar = new Date(data.bar);
12-
if (data.baz) {
13-
for (const key of Object.keys(data.baz)) {
14-
data.baz[key] = fooSchemaResponseTransformer(data.baz[key]);
12+
for (const key of Object.keys(data.baz)) {
13+
data.baz[key] = fooSchemaResponseTransformer(data.baz[key]);
14+
}
15+
if (data.qux) {
16+
for (const key of Object.keys(data.qux)) {
17+
if (!['quux'].includes(key)) {
18+
data.qux[key] = new Date(data.qux[key]);
19+
}
1520
}
1621
}
1722
return data;

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/transformers-additional-properties/types.gen.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ export type Foo = {
1010

1111
export type Bar = {
1212
bar: Date;
13-
baz?: {
13+
baz: {
1414
[key: string]: Foo;
1515
};
16+
qux?: {
17+
quux?: string;
18+
[key: string]: Date | string | undefined;
19+
};
1620
};
1721

1822
export type PostFooData = {

packages/openapi-ts/src/plugins/@hey-api/transformers/plugin.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,24 @@ function processSchemaType({
202202
});
203203

204204
if (entryValueNodes.length) {
205+
const properties = Object.keys(schema.properties ?? {});
205206
nodes.push(
206207
$.for($.const('key'))
207208
.of($('Object').attr('keys').call(dataExpression))
208-
.do(...entryValueNodes),
209+
.$if(
210+
properties.length,
211+
(f) =>
212+
f.do(
213+
$.if(
214+
$.not(
215+
$.array(...properties)
216+
.attr('includes')
217+
.call('key'),
218+
),
219+
).do(...entryValueNodes),
220+
),
221+
(f) => f.do(...entryValueNodes),
222+
),
209223
);
210224
}
211225
}

packages/openapi-ts/src/ts-dsl/expr/array.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import ts from 'typescript';
44
import type { MaybeTsDsl } from '../base';
55
import { TsDsl } from '../base';
66
import { AsMixin } from '../mixins/as';
7+
import { ExprMixin } from '../mixins/expr';
78
import { LayoutMixin } from '../mixins/layout';
89
import { LiteralTsDsl } from './literal';
910

10-
const Mixed = AsMixin(LayoutMixin(TsDsl<ts.ArrayLiteralExpression>));
11+
const Mixed = AsMixin(ExprMixin(LayoutMixin(TsDsl<ts.ArrayLiteralExpression>)));
1112

1213
export class ArrayTsDsl extends Mixed {
1314
readonly '~dsl' = 'ArrayTsDsl';

specs/3.1.x/transformers-additional-properties.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ components:
2626
type: object
2727
required:
2828
- bar
29+
- baz
2930
properties:
3031
bar:
3132
type: string
@@ -34,3 +35,11 @@ components:
3435
type: object
3536
additionalProperties:
3637
$ref: '#/components/schemas/Foo'
38+
qux:
39+
type: object
40+
properties:
41+
quux:
42+
type: string
43+
additionalProperties:
44+
type: string
45+
format: date-time

0 commit comments

Comments
 (0)