Skip to content

Commit

Permalink
Merge pull request #264 from hey-api/fix/model-default
Browse files Browse the repository at this point in the history
fix(parser): use only isRequired to determine if field is required
  • Loading branch information
mrlubos authored Apr 5, 2024
2 parents 1652111 + 12b311f commit 32bb3a6
Show file tree
Hide file tree
Showing 26 changed files with 784 additions and 159 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-worms-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix(parser): use only isRequired to determine if field is required
1 change: 1 addition & 0 deletions packages/openapi-ts/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function handlebarsPlugin(): Plugin {
ifOperationDataOptional: true,
ifdef: true,
intersection: true,
modelIsRequired: true,
modelUnionType: true,
nameOperationDataType: true,
notEquals: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type {{{name}}} = {
{{/if}}
*/
{{/ifdef}}
{{>isReadOnly}}{{{name}}}{{>isRequired}}: {{>type parent=../name}};
{{>isReadOnly}}{{{name}}}{{{modelIsRequired this}}}: {{>type parent=../name}};
{{/each}}
}{{>isNullable}};

Expand Down
5 changes: 0 additions & 5 deletions packages/openapi-ts/src/templates/partials/isRequired.hbs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data: {{#if parameters}}{{{nameOperationDataType @root this}}} & {{/if}}TConfig<
{{#if parameters}}

{{#each parameters}}
{{{name}}}{{>isRequired}}: {{>type}}{{#if default}} = {{{default}}}{{/if}},
{{{name}}}{{{modelIsRequired this}}}: {{>type}}{{#if default}} = {{{default}}}{{/if}},
{{/each}}
{{/if}}
{{/if}}
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/templates/partials/typeInterface.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
*/
{{/ifdef}}
{{#if ../parent}}
{{>isReadOnly}}{{{name}}}{{>isRequired}}: {{>type parent=../parent}};
{{>isReadOnly}}{{{name}}}{{{modelIsRequired this}}}: {{>type parent=../parent}};
{{else}}
{{>isReadOnly}}{{{name}}}{{>isRequired}}: {{>type}};
{{>isReadOnly}}{{{name}}}{{{modelIsRequired this}}}: {{>type}};
{{/if}}
{{/each}}
}{{>isNullable}}
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('registerHandlebarHelpers', () => {
expect(helpers).toContain('ifdef');
expect(helpers).toContain('ifOperationDataOptional');
expect(helpers).toContain('intersection');
expect(helpers).toContain('modelIsRequired');
expect(helpers).toContain('modelUnionType');
expect(helpers).toContain('nameOperationDataType');
expect(helpers).toContain('notEquals');
Expand Down
24 changes: 15 additions & 9 deletions packages/openapi-ts/src/utils/handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import partialExportInterface from '../templates/partials/exportInterface.hbs';
import partialExportType from '../templates/partials/exportType.hbs';
import partialIsNullable from '../templates/partials/isNullable.hbs';
import partialIsReadOnly from '../templates/partials/isReadOnly.hbs';
import partialIsRequired from '../templates/partials/isRequired.hbs';
import partialOperationParameters from '../templates/partials/operationParameters.hbs';
import partialOperationResult from '../templates/partials/operationResult.hbs';
import partialOperationTypes from '../templates/partials/operationTypes.hbs';
Expand Down Expand Up @@ -126,8 +125,12 @@ const dataParameters = (config: Config, parameters: OperationParameter[]) => {
return output.join(', ');
};

// same as `>isRequired` partial
const isRequired = (model: Pick<Model, 'default' | 'isRequired'>) => (model.isRequired && !model.default ? '' : '?');
const modelIsRequired = (config: Config, model: Model) => {
if (config.useOptions) {
return model.isRequired ? '' : '?';
}
return !model.isRequired && !model.default ? '?' : '';
};

const nameOperationDataType = (service: Service, operation: Service['operations'][number]) => {
const namespace = `${camelCase(service.name, { pascalCase: true })}Data`;
Expand All @@ -136,14 +139,14 @@ const nameOperationDataType = (service: Service, operation: Service['operations'
};

export const operationDataType = (config: Config, service: Service) => {
if (!config.useOptions) {
const operationsWithParameters = service.operations.filter(operation => operation.parameters.length);
if (!config.useOptions || !operationsWithParameters.length) {
return '';
}
const partialType = Handlebars.partials['type'];
const namespace = `${camelCase(service.name, { pascalCase: true })}Data`;
const output = `export type ${namespace} = {
${service.operations
.filter(operation => operation.parameters.length)
${operationsWithParameters
.map(
operation => `${camelCase(operation.name, { pascalCase: true })}: {
${sortByName(operation.parameters)
Expand All @@ -160,7 +163,7 @@ export const operationDataType = (config: Config, service: Service) => {
}
return [
...comment,
`${parameter.name + isRequired(parameter)}: ${partialType({ $config: config, ...parameter })}`,
`${parameter.name + modelIsRequired(config, parameter)}: ${partialType({ $config: config, ...parameter })}`,
].join('\n');
})
.join('\n')}
Expand All @@ -176,7 +179,7 @@ export const operationDataType = (config: Config, service: Service) => {
}
return [
...comment,
`${parameter.name + isRequired(parameter)}: ${partialType({ $config: config, ...parameter })}`,
`${parameter.name + modelIsRequired(config, parameter)}: ${partialType({ $config: config, ...parameter })}`,
].join('\n');
})
.join('\n')}
Expand Down Expand Up @@ -270,6 +273,10 @@ export const registerHandlebarHelpers = (config: Config, client: Client): void =
}
);

Handlebars.registerHelper('modelIsRequired', function (model: Model) {
return modelIsRequired(config, model);
});

Handlebars.registerHelper(
'modelUnionType',
function (models: Model[], parent: string | undefined, filterProperties: 'exact' | undefined) {
Expand Down Expand Up @@ -363,7 +370,6 @@ export const registerHandlebarTemplates = (config: Config, client: Client): Temp
Handlebars.registerPartial('exportType', Handlebars.template(partialExportType));
Handlebars.registerPartial('isNullable', Handlebars.template(partialIsNullable));
Handlebars.registerPartial('isReadOnly', Handlebars.template(partialIsReadOnly));
Handlebars.registerPartial('isRequired', Handlebars.template(partialIsRequired));
Handlebars.registerPartial('operationParameters', Handlebars.template(partialOperationParameters));
Handlebars.registerPartial('operationResult', Handlebars.template(partialOperationResult));
Handlebars.registerPartial('operationTypes', Handlebars.template(partialOperationTypes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import type {
NonAsciiStringæøåÆØÅöôêÊ字符串,
} from './models';

export type DefaultData = {};

export type SimpleData = {};

export type DescriptionsData = {
CallWithDescriptions: {
/**
Expand Down Expand Up @@ -109,23 +105,23 @@ export type DefaultsData = {
/**
* This is a simple boolean with default value
*/
parameterBoolean?: boolean;
parameterBoolean: boolean;
/**
* This is a simple enum with default value
*/
parameterEnum?: 'Success' | 'Warning' | 'Error';
parameterEnum: 'Success' | 'Warning' | 'Error';
/**
* This is a simple model with default value
*/
parameterModel?: ModelWithString;
parameterModel: ModelWithString;
/**
* This is a simple number with default value
*/
parameterNumber?: number;
parameterNumber: number;
/**
* This is a simple string with default value
*/
parameterString?: string;
parameterString: string;
};
CallWithDefaultOptionalParameters: {
/**
Expand Down Expand Up @@ -173,30 +169,18 @@ export type DefaultsData = {
/**
* This is a string with default
*/
parameterStringWithDefault?: string;
parameterStringWithDefault: string;
/**
* This is a string with empty default
*/
parameterStringWithEmptyDefault?: string;
parameterStringWithEmptyDefault: string;
/**
* This is a string with no default
*/
parameterStringWithNoDefault: string;
};
};

export type DuplicateData = {};

export type NoContentData = {};

export type ResponseData = {};

export type MultipleTags1Data = {};

export type MultipleTags2Data = {};

export type MultipleTags3Data = {};

export type CollectionFormatData = {
CollectionFormat: {
/**
Expand Down Expand Up @@ -235,7 +219,7 @@ export type TypesData = {
/**
* This is a boolean parameter
*/
parameterBoolean?: boolean;
parameterBoolean: boolean;
/**
* This is a dictionary parameter
*/
Expand All @@ -247,15 +231,15 @@ export type TypesData = {
/**
* This is a number parameter
*/
parameterNumber?: number;
parameterNumber: number;
/**
* This is an object parameter
*/
parameterObject?: unknown;
parameterObject: unknown;
/**
* This is a string parameter
*/
parameterString?: string;
parameterString: string;
};
};

Expand All @@ -278,8 +262,6 @@ export type ComplexData = {
};
};

export type HeaderData = {};

export type ErrorData = {
TestErrorCode: {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export type DefaultData = {
};
};

export type SimpleData = {};

export type ParametersData = {
DeleteFoo: {
/**
Expand Down Expand Up @@ -261,30 +259,18 @@ export type DefaultsData = {
/**
* This is a string with default
*/
parameterStringWithDefault?: string;
parameterStringWithDefault: string;
/**
* This is a string with empty default
*/
parameterStringWithEmptyDefault?: string;
parameterStringWithEmptyDefault: string;
/**
* This is a string with no default
*/
parameterStringWithNoDefault: string;
};
};

export type DuplicateData = {};

export type NoContentData = {};

export type ResponseData = {};

export type MultipleTags1Data = {};

export type MultipleTags2Data = {};

export type MultipleTags3Data = {};

export type CollectionFormatData = {
CollectionFormat: {
/**
Expand Down Expand Up @@ -323,7 +309,7 @@ export type TypesData = {
/**
* This is a boolean parameter
*/
parameterBoolean?: boolean | null;
parameterBoolean: boolean | null;
/**
* This is a dictionary parameter
*/
Expand All @@ -335,15 +321,15 @@ export type TypesData = {
/**
* This is a number parameter
*/
parameterNumber?: number;
parameterNumber: number;
/**
* This is an object parameter
*/
parameterObject?: Record<string, unknown> | null;
parameterObject: Record<string, unknown> | null;
/**
* This is a string parameter
*/
parameterString?: string | null;
parameterString: string | null;
};
};

Expand Down Expand Up @@ -406,8 +392,6 @@ export type MultipartData = {
};
};

export type HeaderData = {};

export type ErrorData = {
TestErrorCode: {
/**
Expand Down
Loading

0 comments on commit 32bb3a6

Please sign in to comment.