-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(cli): improve openapi code generation for naming and typing #2722
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright IBM Corp. 2019. All Rights Reserved. | ||
| // Node module: @loopback/cli | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| const expect = require('@loopback/testlab').expect; | ||
| const utils = require('../../../generators/openapi/utils'); | ||
|
|
||
| describe('openapi utils', () => { | ||
| it('escapes keywords for an identifier', () => { | ||
| expect(utils.escapeIdentifier('for')).to.eql('_for'); | ||
| }); | ||
|
|
||
| it('escapes illegal chars for an identifier', () => { | ||
| expect(utils.escapeIdentifier('foo bar')).to.eql('fooBar'); | ||
| expect(utils.escapeIdentifier('foo-bar')).to.eql('fooBar'); | ||
| expect(utils.escapeIdentifier('foo.bar')).to.eql('fooBar'); | ||
| }); | ||
|
|
||
| it('does not escape legal chars for an identifier', () => { | ||
| expect(utils.escapeIdentifier('foobar')).to.eql('foobar'); | ||
| expect(utils.escapeIdentifier('fooBar')).to.eql('fooBar'); | ||
| expect(utils.escapeIdentifier('Foobar')).to.eql('Foobar'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -282,6 +282,17 @@ describe('schema to model', () => { | |
| declaration: 'string', | ||
| signature: 'Name', | ||
| }, | ||
| { | ||
| description: 'profileId', | ||
| name: 'profileId', | ||
| className: 'ProfileId', | ||
| fileName: 'profile-id.model.ts', | ||
| properties: [], | ||
| imports: [], | ||
| import: "import {ProfileId} from './profile-id.model';", | ||
| declaration: 'string', | ||
| signature: 'ProfileId', | ||
| }, | ||
| { | ||
| description: 'AddressList', | ||
| name: 'Address[]', | ||
|
|
@@ -374,14 +385,19 @@ describe('schema to model', () => { | |
| }, | ||
| { | ||
| name: 'first-name', | ||
| signature: "'first-name'?: string;", | ||
| signature: 'firstName?: string;', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| decoration: "@property({name: 'first-name'})", | ||
| }, | ||
| { | ||
| name: 'last-name', | ||
| signature: "'last-name'?: Name;", | ||
| signature: 'lastName?: Name;', | ||
| decoration: "@property({name: 'last-name'})", | ||
| }, | ||
| { | ||
| name: 'profiles', | ||
| signature: 'profiles?: ProfileId[];', | ||
| decoration: "@property.array(String, {name: 'profiles'})", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am wondering - why are we including IMO, we should exclude Feel free to leave such changes out of scope of this pull request.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of the property names such as
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So how is this going to work in practice? Let's say the OpenAPI schema describes a property called At runtime, this will create object To allow TypeScript code to access this data, the TypeScript property must be called
Are you saying that our code in incorrectly inferring model property name from the TypeScript/JavaScript property name passed by TypeScript to @model()
class MyModel extends Entity {
@property()
'last-name': string;
}
``
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The following code should work: @model()
class MyModel extends Entity {
@property()
'last-name': string;
}But we prefer to support the following: @model({name: 'my-model'})
class MyModel extends Entity {
@property({name: 'last-name'})
lastName: string;
}Mapping between JS/TS and JSON should be allowed. But
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #2743 |
||
| }, | ||
| { | ||
| name: 'emails', | ||
| signature: 'emails?: string[];', | ||
|
|
@@ -392,17 +408,24 @@ describe('schema to model', () => { | |
| signature: 'addresses?: AddressList;', | ||
| decoration: "@property.array(Address, {name: 'addresses'})", | ||
| }, | ||
| { | ||
| name: 'us-office', | ||
| signature: 'usOffice?: Address;', | ||
| decoration: "@property({name: 'us-office'})", | ||
| }, | ||
| ], | ||
| imports: [ | ||
| "import {Name} from './name.model';", | ||
| "import {ProfileId} from './profile-id.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?: AddressList;\n}', | ||
| '{\n id: number;\n firstName?: string;\n lastName?: Name;\n' + | ||
| ' profiles?: ProfileId[];\n emails?: string[];\n' + | ||
| ' addresses?: AddressList;\n usOffice?: Address;\n}', | ||
| signature: 'Customer', | ||
| }, | ||
| ]); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered the following, to avoid duplication of
getJSType?