Skip to content
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

fix: allow arbitrary object properties when additionalProperties is undefined #1390

Merged
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
2 changes: 1 addition & 1 deletion .changeset/clean-worms-sing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'@hey-api/openapi-ts': minor
---

feat: add logs.level option
feat: add `logs.level` option

### Added `logs.level` option

Expand Down
2 changes: 1 addition & 1 deletion .changeset/hungry-dolphins-clap.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@hey-api/openapi-ts': patch
---

fix: add --silent or -s CLI option for silent log level
fix: add `--silent` or `-s` CLI option for silent log level
2 changes: 1 addition & 1 deletion .changeset/moody-falcons-pay.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@hey-api/openapi-ts': patch
---

feat: add logs configuration option to customize log directory
feat: add `logs` configuration option to customize log directory
5 changes: 5 additions & 0 deletions .changeset/proud-socks-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: allow arbitrary object properties when additionalProperties is undefined
2 changes: 1 addition & 1 deletion .changeset/twelve-doors-look.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@hey-api/openapi-ts': patch
---

fix: support DEBUG environment variable
fix: support `DEBUG` environment variable
34 changes: 19 additions & 15 deletions packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,27 @@
irSchema.properties = schemaProperties;
}

if (schema.additionalProperties !== undefined) {
if (typeof schema.additionalProperties === 'boolean') {
if (schema.additionalProperties === undefined) {
if (!irSchema.properties) {

Check warning on line 220 in packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts#L219-L220

Added lines #L219 - L220 were not covered by tests
irSchema.additionalProperties = {
type: schema.additionalProperties ? 'unknown' : 'never',
type: 'unknown',

Check warning on line 222 in packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts#L222

Added line #L222 was not covered by tests
};
} else {
const irAdditionalPropertiesSchema = schemaToIrSchema({
context,
schema: schema.additionalProperties,
});
// no need to add "any" additional properties if there are no defined properties
if (
irSchema.properties ||
irAdditionalPropertiesSchema.type !== 'unknown'
) {
irSchema.additionalProperties = irAdditionalPropertiesSchema;
}
}
} else if (typeof schema.additionalProperties === 'boolean') {
irSchema.additionalProperties = {
type: schema.additionalProperties ? 'unknown' : 'never',
};
} else {
const irAdditionalPropertiesSchema = schemaToIrSchema({
context,
schema: schema.additionalProperties,
});
// no need to add "any" additional properties if there are no defined properties
if (
irSchema.properties ||
irAdditionalPropertiesSchema.type !== 'unknown'
) {
irSchema.additionalProperties = irAdditionalPropertiesSchema;

Check warning on line 239 in packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts#L224-L239

Added lines #L224 - L239 were not covered by tests
}
}

Expand Down
34 changes: 19 additions & 15 deletions packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,23 +269,27 @@ const parseObject = ({
irSchema.properties = schemaProperties;
}

if (schema.additionalProperties !== undefined) {
if (typeof schema.additionalProperties === 'boolean') {
if (schema.additionalProperties === undefined) {
if (!irSchema.properties) {
irSchema.additionalProperties = {
type: schema.additionalProperties ? 'unknown' : 'never',
type: 'unknown',
};
} else {
const irAdditionalPropertiesSchema = schemaToIrSchema({
context,
schema: schema.additionalProperties,
});
// no need to add "any" additional properties if there are no defined properties
if (
irSchema.properties ||
irAdditionalPropertiesSchema.type !== 'unknown'
) {
irSchema.additionalProperties = irAdditionalPropertiesSchema;
}
}
} else if (typeof schema.additionalProperties === 'boolean') {
irSchema.additionalProperties = {
type: schema.additionalProperties ? 'unknown' : 'never',
};
} else {
const irAdditionalPropertiesSchema = schemaToIrSchema({
context,
schema: schema.additionalProperties,
});
// no need to add "any" additional properties if there are no defined properties
if (
irSchema.properties ||
irAdditionalPropertiesSchema.type !== 'unknown'
) {
irSchema.additionalProperties = irAdditionalPropertiesSchema;
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-ts/test/3.0.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ describe(`OpenAPI ${VERSION}`, () => {
}),
description: 'allows arbitrary properties on objects',
},
{
config: createConfig({
input: 'additional-properties-undefined.json',
output: 'additional-properties-undefined',
}),
description: 'allows arbitrary properties on objects',
},
{
config: createConfig({
input: 'array-items-one-of-length-1.json',
Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-ts/test/3.1.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ describe(`OpenAPI ${VERSION}`, () => {
}),
description: 'allows arbitrary properties on objects',
},
{
config: createConfig({
input: 'additional-properties-undefined.json',
output: 'additional-properties-undefined',
}),
description: 'allows arbitrary properties on objects',
},
{
config: createConfig({
input: 'array-items-one-of-length-1.json',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './types.gen';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This file is auto-generated by @hey-api/openapi-ts

export type Foo = {
foo: {
[key: string]: unknown;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,9 @@ export type Pageable = {
/**
* This is a free-form object without additionalProperties.
*/
export type FreeFormObjectWithoutAdditionalProperties = {};
export type FreeFormObjectWithoutAdditionalProperties = {
[key: string]: unknown;
};

/**
* This is a free-form object with additionalProperties: true.
Expand Down Expand Up @@ -1124,7 +1126,9 @@ export type CallWithParametersData = {
/**
* This is the parameter that goes into the body
*/
body: {} | null;
body: {
[key: string]: unknown;
} | null;
headers: {
/**
* This is the parameter that goes into the header
Expand Down Expand Up @@ -1599,15 +1603,19 @@ export type TypesData = {
/**
* This is an object parameter
*/
parameterObject: {} | null;
parameterObject: {
[key: string]: unknown;
} | null;
/**
* This is an array parameter
*/
parameterArray: Array<string> | null;
/**
* This is a dictionary parameter
*/
parameterDictionary: {} | null;
parameterDictionary: {
[key: string]: unknown;
} | null;
/**
* This is an enum parameter
*/
Expand All @@ -1632,7 +1640,9 @@ export type TypesResponses = {
/**
* Response is a simple object
*/
203: {};
203: {
[key: string]: unknown;
};
};

export type TypesResponse = TypesResponses[keyof TypesResponses];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,9 @@ export type Pageable = {
/**
* This is a free-form object without additionalProperties.
*/
export type FreeFormObjectWithoutAdditionalProperties = {};
export type FreeFormObjectWithoutAdditionalProperties = {
[key: string]: unknown;
};

/**
* This is a free-form object with additionalProperties: true.
Expand Down Expand Up @@ -1124,7 +1126,9 @@ export type CallWithParametersData = {
/**
* This is the parameter that goes into the body
*/
body: {} | null;
body: {
[key: string]: unknown;
} | null;
headers: {
/**
* This is the parameter that goes into the header
Expand Down Expand Up @@ -1599,15 +1603,19 @@ export type TypesData = {
/**
* This is an object parameter
*/
parameterObject: {} | null;
parameterObject: {
[key: string]: unknown;
} | null;
/**
* This is an array parameter
*/
parameterArray: Array<string> | null;
/**
* This is a dictionary parameter
*/
parameterDictionary: {} | null;
parameterDictionary: {
[key: string]: unknown;
} | null;
/**
* This is an enum parameter
*/
Expand All @@ -1632,7 +1640,9 @@ export type TypesResponses = {
/**
* Response is a simple object
*/
203: {};
203: {
[key: string]: unknown;
};
};

export type TypesResponse = TypesResponses[keyof TypesResponses];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,9 @@ export type Pageable = {
/**
* This is a free-form object without additionalProperties.
*/
export type FreeFormObjectWithoutAdditionalProperties = {};
export type FreeFormObjectWithoutAdditionalProperties = {
[key: string]: unknown;
};

/**
* This is a free-form object with additionalProperties: true.
Expand Down Expand Up @@ -1124,7 +1126,9 @@ export type CallWithParametersData = {
/**
* This is the parameter that goes into the body
*/
body: {} | null;
body: {
[key: string]: unknown;
} | null;
headers: {
/**
* This is the parameter that goes into the header
Expand Down Expand Up @@ -1599,15 +1603,19 @@ export type TypesData = {
/**
* This is an object parameter
*/
parameterObject: {} | null;
parameterObject: {
[key: string]: unknown;
} | null;
/**
* This is an array parameter
*/
parameterArray: Array<string> | null;
/**
* This is a dictionary parameter
*/
parameterDictionary: {} | null;
parameterDictionary: {
[key: string]: unknown;
} | null;
/**
* This is an enum parameter
*/
Expand All @@ -1632,7 +1640,9 @@ export type TypesResponses = {
/**
* Response is a simple object
*/
203: {};
203: {
[key: string]: unknown;
};
};

export type TypesResponse = TypesResponses[keyof TypesResponses];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,9 @@ export type Pageable = {
/**
* This is a free-form object without additionalProperties.
*/
export type FreeFormObjectWithoutAdditionalProperties = {};
export type FreeFormObjectWithoutAdditionalProperties = {
[key: string]: unknown;
};

/**
* This is a free-form object with additionalProperties: true.
Expand Down Expand Up @@ -1124,7 +1126,9 @@ export type CallWithParametersData = {
/**
* This is the parameter that goes into the body
*/
body: {} | null;
body: {
[key: string]: unknown;
} | null;
headers: {
/**
* This is the parameter that goes into the header
Expand Down Expand Up @@ -1599,15 +1603,19 @@ export type TypesData = {
/**
* This is an object parameter
*/
parameterObject: {} | null;
parameterObject: {
[key: string]: unknown;
} | null;
/**
* This is an array parameter
*/
parameterArray: Array<string> | null;
/**
* This is a dictionary parameter
*/
parameterDictionary: {} | null;
parameterDictionary: {
[key: string]: unknown;
} | null;
/**
* This is an enum parameter
*/
Expand All @@ -1632,7 +1640,9 @@ export type TypesResponses = {
/**
* Response is a simple object
*/
203: {};
203: {
[key: string]: unknown;
};
};

export type TypesResponse = TypesResponses[keyof TypesResponses];
Expand Down
Loading
Loading