SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
805 stars 171 forks source link

update collection throw NullPointerException #241

Closed kevin8023 closed 7 years ago

kevin8023 commented 7 years ago

Hi, when i update collection with below steps, it will throw NullPointerException.

  1. get collection http get http://***:8080/mdm/_schemas/Organization
  2. update collection http put http://***:8080/mdm/_schemas/Organization

-- collection from db as below

 {
    "_embedded": [],
    "_id": "Organization",
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "_id": {
            "type": "object",
            "properties": {
                "$oid": {
                    "type": "string"
                }
            },
            "additionalProperties": false
        },
        "OrganizationName": {
            "type": "string"
        },
        "_etag": {
            "type": "object",
            "properties": {
                "$oid": {
                    "type": "string"
                }
            },
            "additionalProperties": false
        }
    },
    "additionalProperties": false,
    "required": [
        "OrganizationName"
    ],
    "id": "http://schema-store/mdm/Organization#",
    "_etag": {
        "$oid": "59bc9a9e97a1c00005a509f6"
    }
 }

-- nullpointerException as below

 {
    "_exceptions": [{
        "exception": "java.lang.NullPointerException"
    }],
    "http status code": 500,
    "http status description": "Internal Server Error",
    "message": "Error handling the request, see log for more information"
 }
03:15:23.435 [XNIO-1 task-1] ERROR org.restheart.handlers.ErrorHandler - Error handling the request
java.lang.NullPointerException: null
        at org.restheart.handlers.metadata.CheckHandler.lambda$check$0(CheckHandler.java:97)
        at java.util.stream.MatchOps$1MatchSink.accept(MatchOps.java:90)
        at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
        at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
        at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
        at java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:230)
        at java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:196)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.allMatch(ReferencePipeline.java:454)
        at org.restheart.handlers.metadata.CheckHandler.check(CheckHandler.java:94)
        at org.restheart.handlers.metadata.CheckHandler.handleRequest(CheckHandler.java:59)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.handlers.metadata.AbstractTransformerMetadataHandler.handleRequest(AbstractTransformerMetadataHandler.java:62)
        at org.restheart.handlers.RequestDispacherHandler.handleRequest(RequestDispacherHandler.java:468)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.handlers.injectors.CollectionPropsInjectorHandler.handleRequest(CollectionPropsInjectorHandler.java:64)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.handlers.injectors.DbPropsInjectorHandler.handleRequest(DbPropsInjectorHandler.java:55)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.handlers.injectors.AccountInjectorHandler.handleRequest(AccountInjectorHandler.java:56)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.security.handlers.AccessManagerHandler.handleRequest(AccessManagerHandler.java:55)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.security.handlers.AuthTokenInjecterHandler.handleRequest(AuthTokenInjecterHandler.java:70)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.security.handlers.AuthenticationCallHandler.handleRequest(AuthenticationCallHandler.java:54)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:55)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:61)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:85)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.security.handlers.SecurityHandler.handleRequest(SecurityHandler.java:50)
        at org.restheart.security.handlers.SecurityHandlerDispacher.handleRequest(SecurityHandlerDispacher.java:60)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.handlers.injectors.BodyInjectorHandler.handleRequest(BodyInjectorHandler.java:241)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.handlers.OptionsHandler.handleRequest(OptionsHandler.java:58)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.handlers.injectors.RequestContextInjectorHandler.handleRequest(RequestContextInjectorHandler.java:647)
        at org.restheart.handlers.PipedHttpHandler.next(PipedHttpHandler.java:136)
        at org.restheart.security.handlers.CORSHandler.handleRequest(CORSHandler.java:88)
        at org.restheart.handlers.RequestLoggerHandler.handleRequest(RequestLoggerHandler.java:87)
        at org.restheart.handlers.PipedHttpHandler.handleRequest(PipedHttpHandler.java:89)
        at io.undertow.server.handlers.PathHandler.handleRequest(PathHandler.java:94)
        at io.undertow.server.handlers.HttpContinueAcceptingHandler.handleRequest(HttpContinueAcceptingHandler.java:78)
        at org.restheart.handlers.ErrorHandler.handleRequest(ErrorHandler.java:69)
        at io.undertow.server.handlers.encoding.EncodingHandler.handleRequest(EncodingHandler.java:72)
        at org.restheart.handlers.GzipEncodingHandler.handleRequest(GzipEncodingHandler.java:74)
        at io.undertow.server.Connectors.executeRootHandler(Connectors.java:210)
        at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:809)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

if i use below collection to update, it can update successfully. so i found we should use _$oid instead of $oid, could you help us to fixed this issues?

{
    "_embedded": [],
    "_id": "Organization",
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "_id": {
            "type": "object",
            "properties": {
                "_$oid": {
                    "type": "string"
                }
            },
            "additionalProperties": false
        },
        "OrganizationName": {
            "type": "string"
        },
        "_etag": {
            "type": "object",
            "properties": {
                "_$oid": {
                    "type": "string"
                }
            },
            "additionalProperties": false
        }
    },
    "additionalProperties": false,
    "required": [
        "OrganizationName"
    ],
    "id": "http://schema-store/mdm/Organization#",
    "_etag": {
        "$oid": "59bc9a9e97a1c00005a509f6"
    }
}
kevin8023 commented 7 years ago

@mkjsix Hello, any update?

ujibang commented 7 years ago

hi @kevin8023

it took a while to debug this issue....

basically the problem is that the mongodb driver json parser thorws an exception with {"'$oid" : { "type": "string" } }.

this is correct, because $oid is used to represent the bson type ObjectId.

to create the schema you need to underscore prefix it, i.e. _$oid

However the exception was not handled correctly thus returning 500 Internal Server Error It should return 406 Not Acceptable with an error message explaining the problem.

just pushed the fix, you'll find last builds on sonatype soon

I'm closing this, feel free to reopen it if you need more support

mkjsix commented 7 years ago

Hi, We've just released stable version 3.1.3 with same fix.