-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Discriminator validation issue #10655
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
Merged
dhmlau
merged 7 commits into
loopbackio:master
from
pnoebauer:discriminator-validation-issue
Jun 13, 2025
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
46cb9e8
fix: illustrate the discriminator bug and fix in loopback rest reques…
pnoebauer 78c0933
fix: adding tests and comments illustrating the discriminator bug issue
pnoebauer 42b7945
fix: removal of comments and implementation of bug fix
pnoebauer 12964d7
Merge branch 'loopbackio:master' into discriminator-validation-issue
pnoebauer 795f6c9
fix: copyright updates
pnoebauer 1779df1
Merge branch 'loopbackio:master' into discriminator-validation-issue
pnoebauer ea86212
fix: undo copyright updates
pnoebauer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
examples/validation-app/src/__tests__/acceptance/validate-discriminator.acceptance.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved. | ||
| // Node module: @loopback/example-validation-app | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {Client, expect} from '@loopback/testlab'; | ||
| import {ValidationApplication} from '../..'; | ||
| import {setupApplication} from './test-helper'; | ||
|
|
||
| const validCat = { | ||
| name: 'Kitty', | ||
| weight: 5, | ||
| kind: 'Cat', | ||
| animalProperties: { | ||
| color: 'grey', | ||
| whiskerLength: 2, | ||
| }, | ||
| }; | ||
|
|
||
| const validDog = { | ||
| name: 'Rex', | ||
| weight: 5, | ||
| kind: 'Dog', | ||
| animalProperties: { | ||
| breed: 'poodle', | ||
| barkVolume: 5, | ||
| }, | ||
| }; | ||
|
|
||
| describe('validate properties based on discriminated schemas', () => { | ||
| let client: Client; | ||
| let app: ValidationApplication; | ||
|
|
||
| before(givenAClient); | ||
|
|
||
| after(async () => { | ||
| await app.stop(); | ||
| }); | ||
|
|
||
| async function givenAClient() { | ||
| ({app, client} = await setupApplication()); | ||
| } | ||
|
|
||
| it('should pass with valid cat properties', async () => { | ||
| await client.post('/pets').send(validCat).expect(200); | ||
| }); | ||
|
|
||
| it('should pass with valid dog properties', async () => { | ||
| await client.post('/pets').send(validDog).expect(200); | ||
| }); | ||
|
|
||
| it('should fail with error indicating invalid barkVolume type', async () => { | ||
| const invalidDog = {...validDog}; | ||
| invalidDog.animalProperties.barkVolume = 'loud' as unknown as number; | ||
| const response = await client.post('/pets').send(invalidDog).expect(422); | ||
|
|
||
| expect(response.body.error.details.length).to.equal(1); | ||
| expect(response.body.error.details[0].message).to.equal('must be number'); | ||
| expect(response.body.error.details).to.deepEqual([ | ||
| { | ||
| code: 'type', | ||
| info: { | ||
| type: 'number', | ||
| }, | ||
| message: 'must be number', | ||
| path: '/animalProperties/barkVolume', | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should fail with error indicating invalid whiskerLength type', async () => { | ||
| const invalidCat = {...validCat}; | ||
| invalidCat.animalProperties.whiskerLength = 'long' as unknown as number; | ||
| const response = await client.post('/pets').send(invalidCat).expect(422); | ||
|
|
||
| expect(response.body.error.details.length).to.equal(1); | ||
| expect(response.body.error.details[0].message).to.equal('must be number'); | ||
| expect(response.body.error.details).to.deepEqual([ | ||
| { | ||
| code: 'type', | ||
| info: { | ||
| type: 'number', | ||
| }, | ||
| message: 'must be number', | ||
| path: '/animalProperties/whiskerLength', | ||
| }, | ||
| ]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved. | ||
| // Node module: @loopback/example-validation-app | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {post, requestBody} from '@loopback/rest'; | ||
| import {Cat, Dog, Pet} from '../models'; | ||
|
|
||
| export class PetController { | ||
| constructor() {} | ||
|
|
||
| @post('/pets') | ||
| async create( | ||
| @requestBody({ | ||
| content: { | ||
| 'application/json': { | ||
| schema: { | ||
| discriminator: { | ||
| propertyName: 'kind', | ||
| }, | ||
| oneOf: [{'x-ts-type': Cat}, {'x-ts-type': Dog}], | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| request: Pet, | ||
| ): Promise<Pet> { | ||
| return request; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import {Model, model, property} from '@loopback/repository'; | ||
|
|
||
| @model() | ||
| export class CatProperties extends Model { | ||
| @property({ | ||
| type: String, | ||
| required: true, | ||
| }) | ||
| color: string; | ||
|
|
||
| @property({ | ||
| type: Number, | ||
| required: true, | ||
| }) | ||
| whiskerLength: number; | ||
| } | ||
|
|
||
| @model() | ||
| export class DogProperties extends Model { | ||
| @property({ | ||
| type: String, | ||
| required: true, | ||
| }) | ||
| breed: string; | ||
|
|
||
| @property({ | ||
| type: Number, | ||
| required: true, | ||
| }) | ||
| barkVolume: number; | ||
| } | ||
|
|
||
| @model() | ||
| export class Pet extends Model { | ||
| @property({ | ||
| type: String, | ||
| required: true, | ||
| }) | ||
| name: string; | ||
|
|
||
| @property({ | ||
| type: Number, | ||
| required: false, | ||
| }) | ||
| weight?: number; | ||
|
|
||
| kind: string; | ||
|
|
||
| animalProperties: CatProperties | DogProperties; | ||
| } | ||
|
|
||
| @model() | ||
| export class Dog extends Pet { | ||
| @property({ | ||
| type: String, | ||
| jsonSchema: { | ||
| enum: ['Dog'], | ||
| }, | ||
| required: true, | ||
| }) | ||
| kind: string; | ||
|
|
||
| @property({ | ||
| type: DogProperties, | ||
| required: true, | ||
| }) | ||
| animalProperties: DogProperties; | ||
| } | ||
|
|
||
| @model() | ||
| export class Cat extends Pet { | ||
| @property({ | ||
| type: String, | ||
| jsonSchema: { | ||
| enum: ['Cat'], | ||
| }, | ||
| required: true, | ||
| }) | ||
| kind: string; | ||
|
|
||
| @property({ | ||
| type: CatProperties, | ||
| required: true, | ||
| }) | ||
| animalProperties: CatProperties; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/cli/test/fixtures/copyright/single-package/lib/no-header.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
| // Copyright ACME Inc. 2020,2024. All Rights Reserved. | ||
| // Node module: myapp | ||
| // This file is licensed under the ISC License. | ||
| // License text available at https://www.isc.org/licenses/ | ||
|
|
||
| // XYZ | ||
| exports.xyz = {}; | ||
6 changes: 3 additions & 3 deletions
6
packages/cli/test/fixtures/copyright/single-package/src/application.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Copyright ACME Inc. 2020,2024. All Rights Reserved. | ||
pnoebauer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Node module: myapp | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
| // This file is licensed under the ISC License. | ||
pnoebauer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // License text available at https://www.isc.org/licenses/ | ||
|
|
||
| export class MyApplication {} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.