-
Notifications
You must be signed in to change notification settings - Fork 62
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
Support updateOnly feature #92
Conversation
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.
Thank you for the pull request! The changes looks mostly good, I would like you to clean the code a bit.
More importantly, please add a test (or more) to cover this new feature.
lib/specgen/model-helper.js
Outdated
|
||
// check if ModelClass has getUpdateOnlyProperties() function defined to avoid version issues | ||
if (lbdef.modelBuilder && lbdef.modelBuilder.defaultModelBaseClass && | ||
lbdef.modelBuilder.defaultModelBaseClass.getUpdateOnlyProperties) { |
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.
This is getting update-only properties from the default base model, which usually don't contain any, if my understanding is correct. I think you should be calling modelCtor.getUpdateOnlyProperties()
instead. Thoughts?
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.
hmm I added getUpdateOnlyProperties() at the end of last checkins and tested using local mocha test and above code does work in mocha test.. I re-tried it with end to end test creating app using lb app and using api -explorer and I do see that, I need to use modelCtor.getUpdateOnlyProperties() instead.
lib/specgen/model-helper.js
Outdated
var updateOnlyProps = | ||
lbdef.modelBuilder.defaultModelBaseClass.getUpdateOnlyProperties(); | ||
// if at least one updateOnly property is found, we need to create another model $new_xyz to be used only for | ||
// create operation and this model will not have the updateOnly property included in the model. For e.g generated "id" field |
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.
Please wrap the lines at 80 chars
lib/specgen/model-helper.js
Outdated
@@ -50,7 +65,7 @@ var modelHelper = module.exports = { | |||
}, | |||
}; | |||
|
|||
var definitionFunction = function(modelCtor, typeRegistry) { | |||
var definitionFunction = function(modelCtor, typeRegistry, name, updateOnlyProps) { |
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.
I am proposing to add a single argument options
to keep the number of arguments under control. This new argument can expect two properties for the starter - options.name
and options.updateOnlyProps
.
lib/specgen/model-helper.js
Outdated
// if model has updateOnly properties, check if current property we are trying to add is updateOnly. | ||
// If yes, skip adding to the model since this model is used for create operation where generated/update | ||
// only property should not exist | ||
if (updateOnlyProps) { |
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.
I think the definition
function should not need to know that the skipped properties are updateOnly
. In the future, we may want to skip other kinds of properties, e.g. createOnly
. Let's rename updateOnlyProps
to a more generic excludeProps
.
I also think this newly added block can be simplified a bit:
if (excludeProps && excludeProps.includes(key)) {
return;
}
swaggerDef.properties[key] = schema;
@bajtos PTAL
|
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.
Looks mostly good, let's clean up the code a bit more please.
lib/specgen/model-helper.js
Outdated
// check if ModelClass has defined getUpdateOnlyProperties() function to | ||
// avoid version issues | ||
if (modelCtor.getUpdateOnlyProperties) { | ||
var excludeProps = modelCtor.getUpdateOnlyProperties(); |
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.
const excludeProps = ...
lib/specgen/model-helper.js
Outdated
if (excludeProps && excludeProps.length > 0) { | ||
const modelName = '$new_' + name; | ||
typeRegistry.registerModel(modelName, function() { | ||
var options = { |
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.
- use
const
- we don't quote property keys unless they contain characters like space or a dot
- In ES6+, a property name can be excluded when the value is set to a variable with the same name.
const options = {excludeProps};
return definitionFunction(modelCtor, typeRegistry, options);
// or even
return definitionFunction(modelCtor, typeRegistry, {excludeProps});
}); | ||
}); | ||
|
||
function createLoopbackAppWithModelWithForceId(forceId) { |
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.
Instead of copy-pasting most of createLoopbackAppWithModel
, I think it will be better to add a parameter to createLoopbackAppWithModel
.
This is how I envision the new usage:
// replacement for createLoopbackAppWithModel('/custom-api-root');
createLoopbackAppWithModel({apiRoot: '/custom-api-root'});
// in your new tests
createLoopbackAppWithModel({modelOptions.forceId: true});
Also please check why the CI builds are failing. |
@bajtos PTAL. CI will fail until upstream changes in juggler and loopback are merged. 3 failing tests require these changes since these tests check if there is a new model created or not for create operation. |
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.
LGTM with two more suggestions. No further review is necessary, at least as far as I am concerned.
.to.include.members(['Product']); | ||
}); | ||
|
||
it('should use $new_Product definition for post/create operation', function() { |
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.
Please add "when forceId is in effect" and "when forceId is false" to the names of this and the next test. I found it confusing why we say "should use $new_Product" and then "should use Product" without explaining the difference in the tested scenario.
if (options === undefined || options.forceId === undefined) { | ||
modelSettings = {description: ['a-description', 'line2']}; | ||
} else { | ||
modelSettings = {description: ['a-description', 'line2'], forceId: options.forceId}; |
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.
const modelSettings = {description: ['a-description', 'line2']};
if (options && options.forceId !== undefined) {
modelSettings.forceId = options.forceId;
}
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.
Oops, I wanted to approve in my comment above.
@slnode test please |
dba4a26
to
6c64c6b
Compare
This is part of solution forhttps://github.com/strongloop/loopback/issues/2924
Other PRs involved in the solution of issue strongloop/loopback#2924
@bajtos Please take a initial look.
This PR changes include