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(template): support printing exact arrays #46

Merged
merged 1 commit into from
Mar 13, 2024
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
1 change: 1 addition & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const handlebarsPlugin = () => ({
escapeDescription: true,
escapeEnumName: true,
escapeNewline: true,
exactArray: true,
ifdef: true,
ifOperationDataOptional: true,
intersection: true,
Expand Down
11 changes: 10 additions & 1 deletion src/openApi/v3/parser/getModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,16 @@ export const getModel = (
}
}

const arrayItems = getModel(openApi, definition.items);
/**
* if items are a plain array, infer any-of composition
* {@link} https://github.com/ferdikoomen/openapi-typescript-codegen/issues/2062
*/
const arrayItemsDefinition: OpenApiSchema = Array.isArray(definition.items)
? {
anyOf: definition.items,
}
: definition.items;
const arrayItems = getModel(openApi, arrayItemsDefinition);
model.base = arrayItems.base;
model.export = 'array';
model.imports.push(...arrayItems.imports);
Expand Down
4 changes: 4 additions & 0 deletions src/templates/partials/typeArray.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{{~#exactArray this}}
[{{>type link filterProperties="exact"}}]{{>isNullable}}
{{~else~}}
{{~#if link~}}
Array<{{>type link}}>{{>isNullable}}
{{~else~}}
Array<{{>base}}>{{>isNullable}}
{{~/if~}}
{{~/exactArray~}}
2 changes: 1 addition & 1 deletion src/templates/partials/typeUnion.hbs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#union properties parent}}{{this}}{{/union}}{{>isNullable}}
{{#union properties parent filterProperties}}{{this}}{{/union}}{{>isNullable}}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Handlebars from 'handlebars/runtime';

import { HttpClient } from '../HttpClient';
import { registerHandlebarHelpers } from './registerHandlebarHelpers';
import { HttpClient } from '../../HttpClient';
import { registerHandlebarHelpers } from '../registerHandlebarHelpers';

describe('registerHandlebarHelpers', () => {
it('should register the helpers', () => {
Expand All @@ -20,6 +20,7 @@ describe('registerHandlebarHelpers', () => {
expect(helpers).toContain('escapeDescription');
expect(helpers).toContain('escapeEnumName');
expect(helpers).toContain('escapeNewline');
expect(helpers).toContain('exactArray');
expect(helpers).toContain('ifdef');
expect(helpers).toContain('ifOperationDataOptional');
expect(helpers).toContain('intersection');
Expand Down
28 changes: 21 additions & 7 deletions src/utils/registerHandlebarHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ export const registerHandlebarHelpers = (
return value.replace(/\n/g, '\\n');
});

Handlebars.registerHelper('exactArray', function (this: any, model: Model, options: Handlebars.HelperOptions) {
if (model.export === 'array' && model.maxItems && model.minItems && model.maxItems === model.minItems) {
return options.fn(this);
}
return options.inverse(this);
});

Handlebars.registerHelper('ifdef', function (this: any, ...args): string {
const options = args.pop();
if (!args.every(value => !value)) {
Expand Down Expand Up @@ -127,15 +134,22 @@ export const registerHandlebarHelpers = (

Handlebars.registerHelper(
'union',
function (this: any, properties: Model[], parent: string | undefined, options: Handlebars.HelperOptions) {
function (
this: any,
properties: Model[],
parent: string | undefined,
filterProperties: 'exact' | undefined,
options: Handlebars.HelperOptions
) {
const type = Handlebars.partials['type'];
const types = properties.map(property => type({ ...root, ...property, parent }));
const uniqueTypes = types.filter(unique);
let uniqueTypesString = uniqueTypes.join(' | ');
if (uniqueTypes.length > 1) {
uniqueTypesString = `(${uniqueTypesString})`;
const types = properties
.map(property => type({ ...root, ...property, parent }))
.filter((...args) => filterProperties === 'exact' || unique(...args));
let output = types.join(' | ');
if (types.length > 1 && types.length !== properties.length) {
output = `(${output})`;
}
return options.fn(uniqueTypesString);
return options.fn(output);
}
);

Expand Down
Loading
Loading