Skip to content

Commit

Permalink
fix(cli): make sure the item type is imported for an array in openapi…
Browse files Browse the repository at this point in the history
… spec
  • Loading branch information
raymondfeng committed Apr 9, 2019
1 parent bf36532 commit 91b2381
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/cli/generators/openapi/schema-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ function mapObjectType(schema, options) {
if (itemType) {
// Use `@property.array` for array types
propDecoration = `@property.array(${itemType}, {name: '${p}'})`;
collectImports(typeSpec, propertyType.itemType);
}
}
const propSpec = {
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/test/fixtures/openapi/3.0/customer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ components:
schemas:
Name:
type: string
AddressList:
items:
$ref: '#/components/schemas/Address'
type: array
Address:
type: object
properties:
Expand Down Expand Up @@ -122,6 +126,4 @@ components:
items:
type: string
addresses:
type: array
items:
$ref: '#/components/schemas/Address'
$ref: '#/components/schemas/AddressList'
55 changes: 50 additions & 5 deletions packages/cli/test/unit/openapi/schema-model.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,51 @@ describe('schema to model', () => {
declaration: 'string',
signature: 'Name',
},
{
description: 'AddressList',
name: 'Address[]',
className: 'AddressList',
fileName: 'address-list.model.ts',
properties: [],
imports: ["import {Address} from './address.model';"],
import: "import {AddressList} from './address-list.model';",
declaration: 'Address[]',
signature: 'AddressList',
itemType: {
description: 'Address',
name: 'Address',
className: 'Address',
fileName: 'address.model.ts',
properties: [
{
name: 'street',
signature: 'street?: string;',
decoration: "@property({name: 'street'})",
},
{
name: 'city',
signature: 'city?: string;',
decoration: "@property({name: 'city'})",
},
{
name: 'state',
signature: 'state?: string;',
decoration: "@property({name: 'state'})",
},
{
name: 'zipCode',
signature: 'zipCode?: string;',
decoration: "@property({name: 'zipCode'})",
},
],
imports: [],
import: "import {Address} from './address.model';",
kind: 'class',
declaration:
'{\n street?: string;\n city?: string;\n state?: string;\n zipCode?: string;\n}',
signature: 'Address',
},
},
{
description: 'Address',
name: 'Address',
Expand Down Expand Up @@ -313,8 +358,7 @@ describe('schema to model', () => {
import: "import {Address} from './address.model';",
kind: 'class',
declaration:
'{\n street?: string;\n city?: string;\n state?: string;\n ' +
'zipCode?: string;\n}',
'{\n street?: string;\n city?: string;\n state?: string;\n zipCode?: string;\n}',
signature: 'Address',
},
{
Expand All @@ -339,25 +383,26 @@ describe('schema to model', () => {
decoration: "@property({name: 'last-name'})",
},
{
decoration: "@property.array(String, {name: 'emails'})",
name: 'emails',
signature: 'emails?: string[];',
decoration: "@property.array(String, {name: 'emails'})",
},
{
name: 'addresses',
signature: 'addresses?: Address[];',
signature: 'addresses?: AddressList;',
decoration: "@property.array(Address, {name: 'addresses'})",
},
],
imports: [
"import {Name} from './name.model';",
"import {Address} from './address.model';",
"import {AddressList} from './address-list.model';",
],
import: "import {Customer} from './customer.model';",
kind: 'class',
declaration:
"{\n id: number;\n 'first-name'?: string;\n 'last-name'?: Name;\n" +
' emails?: string[];\n addresses?: Address[];\n}',
' emails?: string[];\n addresses?: AddressList;\n}',
signature: 'Customer',
},
]);
Expand Down

0 comments on commit 91b2381

Please sign in to comment.