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

feat: combine 'additionalProperties: true' with explicit properties #1808

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 18 additions & 4 deletions src/openApi/v3/parser/getModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ export const getModel = (
}
}

if (
definition.type === 'object' &&
(typeof definition.additionalProperties === 'object' || definition.additionalProperties === true)
) {
if (definition.type === 'object' && typeof definition.additionalProperties === 'object') {
const ap = typeof definition.additionalProperties === 'object' ? definition.additionalProperties : {};
if (ap.$ref) {
const additionalProperties = getType(ap.$ref);
Expand Down Expand Up @@ -165,6 +162,23 @@ export const getModel = (
model.enums.push(modelProperty);
}
});

if (definition.additionalProperties === true) {
const additionalPropertiesModel = JSON.parse(JSON.stringify(model)); //deepcopy
const additionalProperties = getModel(openApi, {});
additionalPropertiesModel.name = '[key: string]';
additionalPropertiesModel.export = 'generic';
additionalPropertiesModel.type = 'any';
additionalPropertiesModel.base = 'any';
additionalPropertiesModel.isRequired = true;
additionalPropertiesModel.template = additionalProperties.template;
additionalPropertiesModel.link = additionalProperties;
additionalPropertiesModel.imports.push(...additionalProperties.imports);
additionalPropertiesModel.default = getModelDefault(definition, model);
additionalPropertiesModel.description = null;
model.properties.push(additionalPropertiesModel);
}

return model;
} else {
const additionalProperties = getModel(openApi, {});
Expand Down
42 changes: 42 additions & 0 deletions test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3707,6 +3707,7 @@ export type { ModelCircle } from './models/ModelCircle';
export type { ModelSquare } from './models/ModelSquare';
export type { ModelThatExtends } from './models/ModelThatExtends';
export type { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends';
export type { ModelWithAdditionalPropertiesEqTrue } from './models/ModelWithAdditionalPropertiesEqTrue';
export type { ModelWithArray } from './models/ModelWithArray';
export type { ModelWithBoolean } from './models/ModelWithBoolean';
export type { ModelWithCircularReference } from './models/ModelWithCircularReference';
Expand Down Expand Up @@ -3777,6 +3778,7 @@ export { $ModelCircle } from './schemas/$ModelCircle';
export { $ModelSquare } from './schemas/$ModelSquare';
export { $ModelThatExtends } from './schemas/$ModelThatExtends';
export { $ModelThatExtendsExtends } from './schemas/$ModelThatExtendsExtends';
export { $ModelWithAdditionalPropertiesEqTrue } from './schemas/$ModelWithAdditionalPropertiesEqTrue';
export { $ModelWithArray } from './schemas/$ModelWithArray';
export { $ModelWithBoolean } from './schemas/$ModelWithBoolean';
export { $ModelWithCircularReference } from './schemas/$ModelWithCircularReference';
Expand Down Expand Up @@ -4504,6 +4506,25 @@ export type ModelThatExtendsExtends = (ModelWithString & ModelThatExtends & {
"
`;

exports[`v3 should generate: test/generated/v3/models/ModelWithAdditionalPropertiesEqTrue.ts 1`] = `
"/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
/**
* This is a model with one property and additionalProperties: true
*/
export type ModelWithAdditionalPropertiesEqTrue = {
/**
* This is a simple string property
*/
prop?: string;
[key: string]: any;
};

"
`;

exports[`v3 should generate: test/generated/v3/models/ModelWithArray.ts 1`] = `
"/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
Expand Down Expand Up @@ -5802,6 +5823,27 @@ export const $ModelThatExtendsExtends = {
"
`;

exports[`v3 should generate: test/generated/v3/schemas/$ModelWithAdditionalPropertiesEqTrue.ts 1`] = `
"/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
export const $ModelWithAdditionalPropertiesEqTrue = {
description: \`This is a model with one property and additionalProperties: true\`,
properties: {
prop: {
type: 'string',
description: \`This is a simple string property\`,
},
[key: string]: {
type: 'any',
isRequired: true,
},
},
} as const;
"
`;

exports[`v3 should generate: test/generated/v3/schemas/$ModelWithArray.ts 1`] = `
"/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
Expand Down
11 changes: 11 additions & 0 deletions test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -2549,6 +2549,17 @@
"type": "object",
"additionalProperties": true
},
"ModelWithAdditionalPropertiesEqTrue": {
"description": "This is a model with one property and additionalProperties: true",
"type": "object",
"properties": {
"prop": {
"description": "This is a simple string property",
"type": "string"
}
},
"additionalProperties": true
},
"FreeFormObjectWithAdditionalPropertiesEqEmptyObject": {
"description": "This is a free-form object with additionalProperties: {}.",
"type": "object",
Expand Down