Skip to content

Commit

Permalink
Merge pull request #1390 from hey-api/fix/experimental-parser-additio…
Browse files Browse the repository at this point in the history
…nal-properties-undefined

fix: allow arbitrary object properties when additionalProperties is undefined
  • Loading branch information
mrlubos authored Dec 4, 2024
2 parents 7ee3960 + 8388c47 commit 311b863
Show file tree
Hide file tree
Showing 50 changed files with 631 additions and 206 deletions.
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 @@ 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
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

0 comments on commit 311b863

Please sign in to comment.