Skip to content

Commit

Permalink
fix(parser): do not ignore additionalProperties when object with prop…
Browse files Browse the repository at this point in the history
…erties
  • Loading branch information
mrlubos committed Apr 9, 2024
1 parent 22fc6f7 commit e3df255
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-tips-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: do not ignore additionalProperties when object with properties object
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/openApi/v3/parser/getModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const getModel = (
}
});

if (definition.additionalProperties === true) {
if (definition.additionalProperties) {
const modelProperty = getAdditionalPropertiesModel(openApi, definition, getModel, model);
model.properties.push(modelProperty);
}
Expand Down
16 changes: 8 additions & 8 deletions packages/openapi-ts/src/openApi/v3/parser/getModelProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ export const getAdditionalPropertiesModel = (
const ap = typeof definition.additionalProperties === 'object' ? definition.additionalProperties : {};
const apModel = getModel(openApi, ap);

if (definition.additionalProperties === true && definition.properties) {
apModel.default = getDefault(definition, model);
apModel.export = 'generic';
apModel.isRequired = true;
apModel.name = '[key: string]';
return apModel;
}

if (ap.$ref) {
const apType = getType(ap.$ref);
model.base = apType.base;
Expand All @@ -39,6 +31,14 @@ export const getAdditionalPropertiesModel = (
return model;
}

if (definition.additionalProperties && definition.properties) {
apModel.default = getDefault(definition, model);
apModel.export = 'generic';
apModel.isRequired = definition.additionalProperties === true;
apModel.name = '[key: string]';
return apModel;
}

model.base = apModel.base;
model.default = getDefault(definition, model);
model.export = 'dictionary';
Expand Down
8 changes: 3 additions & 5 deletions packages/openapi-ts/src/utils/write/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,24 @@ const processEnum = (config: Config, client: Client, model: Model, exportType: b
return nodes;
};

const processInterface = processComposition;

const processType = (config: Config, client: Client, model: Model) => {
const comment: Comments = [
model.description && ` * ${escapeComment(model.description)}`,
model.deprecated && ' * @deprecated',
];
return compiler.typedef.alias(model.name, toType(model, config)!, comment);
const type = toType(model, config);
return compiler.typedef.alias(model.name, type!, comment);
};

const processModel = (config: Config, client: Client, model: Model) => {
switch (model.export) {
case 'all-of':
case 'any-of':
case 'one-of':
case 'interface':
return processComposition(config, client, model);
case 'enum':
return processEnum(config, client, model, true);
case 'interface':
return processInterface(config, client, model);
default:
return processType(config, client, model);
}
Expand Down
29 changes: 18 additions & 11 deletions packages/openapi-ts/src/utils/write/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { unique } from '../unique';
const base = (model: Model, config: Config) => {
if (model.base === 'binary') {
return 'Blob | File';
} else {
if (config.useDateType && model.format === 'date-time') {
return 'Date';
} else {
return model.base;
}
}

if (config.useDateType && model.format === 'date-time') {
return 'Date';
}

return model.base;

Check warning on line 18 in packages/openapi-ts/src/utils/write/type.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/type.ts#L13-L18

Added lines #L13 - L18 were not covered by tests
};

const typeReference = (model: Model, config: Config) => `${base(model, config)}${model.isNullable ? ' | null' : ''}`;
Expand Down Expand Up @@ -73,15 +73,22 @@ const typeInterface = (model: Model, config: Config) => {

return `{
${model.properties
.map(m => {
.map(property => {

Check warning on line 76 in packages/openapi-ts/src/utils/write/type.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/type.ts#L76

Added line #L76 was not covered by tests
let s = '';
if (m.description || m.deprecated) {
if (property.description || property.deprecated) {

Check warning on line 78 in packages/openapi-ts/src/utils/write/type.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/type.ts#L78

Added line #L78 was not covered by tests
s += addLeadingJSDocComment(undefined, [
m.description && ` * ${escapeComment(m.description)}`,
m.deprecated && ` * @deprecated`,
property.description && ` * ${escapeComment(property.description)}`,
property.deprecated && ` * @deprecated`,

Check warning on line 81 in packages/openapi-ts/src/utils/write/type.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/type.ts#L80-L81

Added lines #L80 - L81 were not covered by tests
]);
}
s += `${m.isReadOnly ? 'readonly ' : ''}${m.name}${modelIsRequired(config, m)}: ${toType(m, config)}`;
let maybeRequired = modelIsRequired(config, property);
let value = toType(property, config);
// special case for additional properties type
if (property.name === '[key: string]' && maybeRequired) {
maybeRequired = '';
value = `${value} | undefined`;
}
s += `${property.isReadOnly ? 'readonly ' : ''}${property.name}${maybeRequired}: ${value}`;

Check warning on line 91 in packages/openapi-ts/src/utils/write/type.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/type.ts#L84-L91

Added lines #L84 - L91 were not covered by tests
return s;
})
.join('\n')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ export type AnyOfArrays = {
*/
export type DictionaryWithString = Record<string, string>;

export type DictionaryWithPropertiesAndAdditionalProperties = {
foo?: string;
[key: string]: string | undefined;
};

/**
* This is a string reference
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ export const $DictionaryWithString = {
},
} as const;

export const $DictionaryWithPropertiesAndAdditionalProperties = {
type: 'object',
properties: {
foo: {
type: 'string',
},
},
additionalProperties: {
type: 'string',
},
} as const;

export const $DictionaryWithReference = {
description: 'This is a string reference',
type: 'object',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ export type AnyOfArrays = {
*/
export type DictionaryWithString = Record<string, string>;

export type DictionaryWithPropertiesAndAdditionalProperties = {
foo?: string;
[key: string]: string | undefined;
};

/**
* This is a string reference
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ export const $DictionaryWithString = {
},
} as const;

export const $DictionaryWithPropertiesAndAdditionalProperties = {
type: 'object',
properties: {
foo: {
type: 'string',
},
},
additionalProperties: {
type: 'string',
},
} as const;

export const $DictionaryWithReference = {
description: 'This is a string reference',
type: 'object',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ export type AnyOfArrays = {
*/
export type DictionaryWithString = Record<string, string>;

export type DictionaryWithPropertiesAndAdditionalProperties = {
foo?: string;
[key: string]: string | undefined;
};

/**
* This is a string reference
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ export const $DictionaryWithString = {
},
} as const;

export const $DictionaryWithPropertiesAndAdditionalProperties = {
type: 'object',
properties: {
foo: {
type: 'string',
},
},
additionalProperties: {
type: 'string',
},
} as const;

export const $DictionaryWithReference = {
description: 'This is a string reference',
type: 'object',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ export type AnyOfArrays = {
*/
export type DictionaryWithString = Record<string, string>;

export type DictionaryWithPropertiesAndAdditionalProperties = {
foo?: string;
[key: string]: string | undefined;
};

/**
* This is a string reference
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ export const $DictionaryWithString = {
},
} as const;

export const $DictionaryWithPropertiesAndAdditionalProperties = {
type: 'object',
properties: {
foo: {
type: 'string',
},
},
additionalProperties: {
type: 'string',
},
} as const;

export const $DictionaryWithReference = {
description: 'This is a string reference',
type: 'object',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ export type AnyOfArrays = {
*/
export type DictionaryWithString = Record<string, string>;

export type DictionaryWithPropertiesAndAdditionalProperties = {
foo?: string;
[key: string]: string | undefined;
};

/**
* This is a string reference
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ export const $DictionaryWithString = {
},
} as const;

export const $DictionaryWithPropertiesAndAdditionalProperties = {
type: 'object',
properties: {
foo: {
type: 'string',
},
},
additionalProperties: {
type: 'string',
},
} as const;

export const $DictionaryWithReference = {
description: 'This is a string reference',
type: 'object',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ export type AnyOfArrays = {
*/
export type DictionaryWithString = Record<string, string>;

export type DictionaryWithPropertiesAndAdditionalProperties = {
foo?: string;
[key: string]: string | undefined;
};

/**
* This is a string reference
*/
Expand Down
11 changes: 11 additions & 0 deletions packages/openapi-ts/test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,17 @@
"type": "string"
}
},
"DictionaryWithPropertiesAndAdditionalProperties": {
"type": "object",
"properties": {
"foo": {
"type": "string"
}
},
"additionalProperties": {
"type": "string"
}
},
"DictionaryWithReference": {
"description": "This is a string reference",
"type": "object",
Expand Down

0 comments on commit e3df255

Please sign in to comment.