Skip to content

Commit ec80d9a

Browse files
committed
fix(cli): generate property.array for array of simple types
1 parent a8c5983 commit ec80d9a

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

packages/cli/generators/openapi/schema-helper.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,15 @@ function mapObjectType(schema, options) {
167167
propDecoration = `@property({name: '${p}', required: true})`;
168168
}
169169

170-
if (propertyType.itemType && propertyType.itemType.kind === 'class') {
171-
// Use `@property.array` for array types
172-
propDecoration = `@property.array(${
173-
propertyType.itemType.className
174-
}, {name: '${p}'})`;
170+
if (propertyType.itemType) {
171+
const itemType =
172+
propertyType.itemType.kind === 'class'
173+
? propertyType.itemType.className
174+
: getJSType(propertyType.itemType.name);
175+
if (itemType) {
176+
// Use `@property.array` for array types
177+
propDecoration = `@property.array(${itemType}, {name: '${p}'})`;
178+
}
175179
}
176180
const propSpec = {
177181
name: p,
@@ -258,6 +262,23 @@ function mapPrimitiveType(schema, options) {
258262
return typeSpec;
259263
}
260264

265+
const JSTypeMapping = {
266+
number: Number,
267+
boolean: Boolean,
268+
string: String,
269+
Date: Date,
270+
Buffer: Buffer,
271+
};
272+
273+
/**
274+
* Mapping simple type names to JS Type constructors
275+
* @param {string} type Simple type name
276+
*/
277+
function getJSType(type) {
278+
const ctor = JSTypeMapping[type];
279+
return (ctor && ctor.name) || type;
280+
}
281+
261282
/**
262283
*
263284
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#data-types

packages/cli/test/fixtures/openapi/3.0/customer.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
openapi: "3.0.0"
1+
openapi: '3.0.0'
22
info:
33
version: 1.0.0
44
title: Customer
@@ -117,9 +117,11 @@ components:
117117
type: string
118118
last-name:
119119
$ref: '#/components/schemas/Name'
120+
emails:
121+
type: array
122+
items:
123+
type: string
120124
addresses:
121125
type: array
122126
items:
123127
$ref: '#/components/schemas/Address'
124-
125-

packages/cli/test/unit/openapi/schema-model.unit.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const {
1414
const path = require('path');
1515

1616
describe('schema to model', () => {
17-
let usptoSpec, usptoSpecAnonymous, petstoreSpec, customerSepc;
17+
let usptoSpec, usptoSpecAnonymous, petstoreSpec, customerSpec;
1818
const uspto = path.join(__dirname, '../../fixtures/openapi/3.0/uspto.yaml');
1919
const petstore = path.join(
2020
__dirname,
@@ -31,7 +31,7 @@ describe('schema to model', () => {
3131
promoteAnonymousSchemas: true,
3232
});
3333
petstoreSpec = await loadSpec(petstore);
34-
customerSepc = await loadSpec(customer);
34+
customerSpec = await loadSpec(customer);
3535
});
3636

3737
it('generates models for uspto', () => {
@@ -269,7 +269,7 @@ describe('schema to model', () => {
269269

270270
it('generates models for customer', () => {
271271
const objectTypeMapping = new Map();
272-
const models = generateModelSpecs(customerSepc, {objectTypeMapping});
272+
const models = generateModelSpecs(customerSpec, {objectTypeMapping});
273273
expect(models).to.eql([
274274
{
275275
description: 'Name',
@@ -338,6 +338,11 @@ describe('schema to model', () => {
338338
signature: "'last-name'?: Name;",
339339
decoration: "@property({name: 'last-name'})",
340340
},
341+
{
342+
decoration: "@property.array(String, {name: 'emails'})",
343+
name: 'emails',
344+
signature: 'emails?: string[];',
345+
},
341346
{
342347
name: 'addresses',
343348
signature: 'addresses?: Address[];',
@@ -351,8 +356,8 @@ describe('schema to model', () => {
351356
import: "import {Customer} from './customer.model';",
352357
kind: 'class',
353358
declaration:
354-
"{\n id: number;\n 'first-name'?: string;\n 'last-name'?: " +
355-
'Name;\n addresses?: Address[];\n}',
359+
"{\n id: number;\n 'first-name'?: string;\n 'last-name'?: Name;\n" +
360+
' emails?: string[];\n addresses?: Address[];\n}',
356361
signature: 'Customer',
357362
},
358363
]);

0 commit comments

Comments
 (0)