-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
getModelSchemaRef is not returning a property from a model that extends an entity #4388
Comments
Hey @rodrigomf24 Try comparing your implementation to the shopping example. If there is a problem with the documentation please point it out. I'd be happy to take a look at your code if you can provide a repo replicating your issue(upload to git as public repository). |
Looks like a duplicate of #3293 to me. |
@dougal83 here is the link to the repository https://github.com/rodrigomf24/medipharma-api, I will compare it today, but @ricky92's comment makes sense, I will try this solution:
|
Looking at the code, it is working as expected. Also typically we do not return a password due to it's sensitive nature. Looking at the controller you are actually returning the @post('/users')
async create( ... ): Promise<User> If you want to return the password(not a good idea): @post('/users', {
responses: {
'200': {
description: 'NewUserWITHPASSWORDHASH!',
content: {
'application/json': {
schema: getModelSchemaRef(NewUserRequest),
},
},
},
},
})
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(NewUserRequest, {
title: 'NewUser',
exclude: ['id'],
}),
},
},
})
newUserRequest: NewUserRequest,
): Promise<NewUserRequest> {
// ensure a valid email value and password value
validateCredentials(_.pick(newUserRequest, ['email', 'password']));
// encrypt the password
const password = await this.passwordHasher.hashPassword(
newUserRequest.password,
);
try {
// create the new user
const savedUser = await this.userRepository.create(
_.omit(newUserRequest, 'password'),
);
// set the password
await this.userRepository
.userCredentials(savedUser.id)
.create({password});
const newUser = new NewUserRequest(savedUser);
newUser.password = password;
return newUser;
} catch (error) {
...
}
} Hope that clears up what is going on here. |
Thanks @ricky92. I think your issue is loosely related but this particular case is down to the controller returning a model that doesn't contain the include. |
Hi @dougal83, thanks for taking a look at the code. I am not trying to return the password, the problem I have is when I try to create a new user I want it to use the |
@rodrigomf24 Oh right, I think I understand you now. I was over-complicating the issue in my head. const savedUser = await this.userRepository.create(); Your code is calling |
Potentially related to #4547 |
@dougal83 I agree with you. When I add a relation with User and use "includeRelations" option at responses, the password is disappear. |
Looking further into this issue... I presume the main stumbling block may be around the requirement to register an inclusion resolver before it can be used. Please take a read of the documentation and see if it helps or if you could suggest an improvement? I had a go at altering the loopback4 shopping example and found that the inclusion resolves if I add the following line after the relation is configured: this.registerInclusionResolver('userCredentials', this.userCredentials.inclusionResolver); To test I created an endpoint to consume the filter: @get('/users', {
responses: {
'200': {
description: 'Array of User model instances',
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(User, { includeRelations: true }),
},
},
},
},
},
})
async find(
@param.query.object('filter', getFilterSchemaFor(User))
filter?: Filter<User>,
): Promise<User[]> {
return this.userRepository.find(filter);
} Finally I queried via browser: http://localhost:3000/users?filter[include][0][relation]=userCredentials Does that give you a starting point? IMO it is most likely the inclusion registration being the issue. |
Closing. Feel free to reopen as necessary. |
Steps to reproduce
lb4 relation
command to create a hasMany relationship(user has many > new model)Current Behavior
password
property from the NewUserRequest model passed togetModelSchemaRef
for the request body of the userspost
endpoint. When the create endpoint handler is tested through the explorer API and thepassword
attribute is passed theadditionalProperties
error is raised, when it is not passed it raises a validation error:Expected Behavior
password
field as required and as part of the schema.Link to reproduction sandbox
Additional information
This is the model passed to
getModelSchemaRef
:This is the User model:
and this is the user repository
This is the user controller post/create definition
Related Issues
See Reporting Issues for more tips on writing good issues
The text was updated successfully, but these errors were encountered: