kazu2012 / persevere-framework

Automatically exported from code.google.com/p/persevere-framework
0 stars 0 forks source link

POST creates instances/records even if (non-optional) fields are missing #275

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Create a class with a non-optional property, e.g.:
    Class({
        id: 'Stock',
        instances: { '$ref': 'Stock/' },
        properties: {
            symbol: { type: 'string', unique: true },
        },
    });
2. Try to create an instance using the JS console, which won't work:
js>new Stock
TypeError: A value is required for property symbol

3. Try to create an instance using POST (or, more accurately, curl):
$ curl -d '{test: true}' localhost:8080/Stock

Expected: Failure (e.g. HTTP Status Code 400) in Step 3, similar to Step 2. 
However, the HTTP Response is:
HTTP/1.1 201 Created
Date: Thu, 22 Jul 2010 00:02:56 GMT
Server: Persevere
Last-Modified: Thu, 22 Jul 2010 03:02:56.657 EEST
Location: http://localhost:8080/Stock/3
Content-Type: application/javascript; charset=UTF-8;schema=Class/Stock
Transfer-Encoding: chunked

({"id":"Stock/3",
"test":true
})

FWIW, the JS console logs:
2010-07-22 03:02:56.663::WARN:  /Stock
org.mozilla.javascript.EcmaError: TypeError: A value is required for property 
symbol
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3654)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3632)
    at org.persvr.data.PersistableClass.addPropertyToValidationError(PersistableClass.java:354)
    at org.persvr.data.PersistableClass.enforceSchemaForProperty(PersistableClass.java:604)
    at org.persvr.data.PersistableClass.enforceObjectIsValidBySchema(PersistableClass.java:624)
    at org.persvr.data.RestMethod$4.runMethod(RestMethod.java:143)
    at org.persvr.data.RestMethod.call(RestMethod.java:29)
    at org.persvr.data.Method.callWithChecks(Method.java:140)
    at org.persvr.data.Method.call(Method.java:94)
    at org.persvr.data.Transaction.restActions(Transaction.java:136)
    at org.persvr.data.Transaction.doCommit(Transaction.java:261)
    at org.persvr.data.Transaction.commitIfReady(Transaction.java:231)
    at org.persvr.data.Transaction.syncCommit(Transaction.java:214)
    at org.persvr.data.Transaction.commit(Transaction.java:194)
    at org.persvr.remote.PersevereFilter.doFilter(PersevereFilter.java:439)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1115)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:361)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:417)
    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:534)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:879)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:741)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:213)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:403)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:522)

Even stranger, the new item doesn't appear when loading 'Stock/' (or 
Stock.instances in JS), but loading the specific item [Stock/3] does return the 
(malformed) item!

Version: 1.0.2rc1

Original issue reported on code.google.com by remoun.metyas on 22 Jul 2010 at 12:10

GoogleCodeExporter commented 8 years ago
Figured out a solution: Calling PersistableClass.enforceObjectIsValidBySchema 
in PersevereFilter.postObject().

Attached is a patch, please review.

Original comment by remoun.metyas on 22 Jul 2010 at 9:06

Attachments:

GoogleCodeExporter commented 8 years ago
After some more testing with the patch from my previous reply, the last note in 
the original bug report still exists:
> Even stranger, the new item doesn't appear when loading 'Stock/' (or 
Stock.instances in JS), but loading the specific item [e.g. Stock/3] does 
return the (malformed) item!

The POST does result in a 400, but the obj seems to be held in memory -- GET by 
ID shows the malformed item, but it goes away after restarting Persevere. The 
ID is also skipped over when the next valid instance is creating (e.g. if 
max(ID) = 18 when a bad request is made, Stock/19 would still return the 
malformed object, until Persevere is restarted. But even after it is restarted, 
the next object would get ID 20).

Might this have anything to do with JavascriptDB holding on to references until 
a commit?

Original comment by remoun.metyas on 23 Jul 2010 at 6:37