org.mozilla.javascript.EvaluatorException: Cannot add a property to a sealed object.

182 views
Skip to first unread message

Gwyn Jensen

unread,
Apr 15, 2014, 7:37:27 AM4/15/14
to json-schem...@googlegroups.com
When I attempt to validate a json string against a schema that uses a regex pattern on a string property, I get the above Exception.
The example below is a simple test case that will reproduce it.

@Test
public void test() throws Exception {

String content = "{\"employer\" :{\"tradeClass\": \"I3000\"}}";
String schemaText = "{\"$schema\": \"http://json-schema.org/draft-04/schema#\", " +
"\"type\":\"object\",\"properties\": {\"employer\":{\"type\":\"object\"," +
"\"properties\":{\"tradeClass\": {\"type\":\"string\",\"pattern\":\"[A-Z]\\\\d{4}\" }}}}}";
JsonNode schemaNode = JsonLoader.fromString(text);
JsonNode data = JsonLoader.fromString(content);  
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonSchema schema = factory.getJsonSchema(schemaNode);
ProcessingReport report = schema.validate(data);
if (report != null) {
                Iterator<ProcessingMessage> iter = report.iterator();
       while (iter.hasNext()) {
           ProcessingMessage pm = iter.next();
           //TODO: Add error to a failure message
           System.out.println("Processing Message: "+pm.getMessage());
       }
       Assert.assertTrue(report.isSuccess());
       }
}

Can any one help me find the cause please?
The actual schema I am validating against is much larger than this and the test succeeds when I remove all pattern attributes. 
The same simple schema and json validate successfully using the online validator page so I assume I am missing something straight forward here.

Thanks
Gwyn

Francis Galiegue

unread,
Apr 15, 2014, 7:55:38 AM4/15/14
to Gwyn Jensen, json-schem...@googlegroups.com
Hello,
This is the first time I see this error! Can you paste the full stack trace?

I suspect this occurs when evaluating whether the regex is valid. Not
sure though. I'll try and reproduce...

--
Francis Galiegue, fgal...@gmail.com
JSON Schema in Java: http://json-schema-validator.herokuapp.com

Francis Galiegue

unread,
Apr 15, 2014, 8:02:18 AM4/15/14
to Gwyn Jensen, json-schem...@googlegroups.com
On Tue, Apr 15, 2014 at 1:55 PM, Francis Galiegue <fgal...@gmail.com> wrote:
> Hello,
>
> On Tue, Apr 15, 2014 at 1:37 PM, Gwyn Jensen <gwyn....@googlemail.com> wrote:
>> When I attempt to validate a json string against a schema that uses a regex
>> pattern on a string property, I get the above Exception.
>> The example below is a simple test case that will reproduce it.
>>

Can't reproduce. This seems to be a rhino error. Do you have another
rhino version in your classpath? This package uses 1.7R4.

Francis Galiegue

unread,
Apr 15, 2014, 8:10:23 AM4/15/14
to Gwyn Jensen, json-schem...@googlegroups.com
On Tue, Apr 15, 2014 at 1:37 PM, Gwyn Jensen <gwyn....@googlemail.com> wrote:
[...]

> if (report != null) {

Not needed: report can never be null.

Francis Galiegue

unread,
Apr 15, 2014, 8:18:23 AM4/15/14
to Gwyn Jensen, json-schem...@googlegroups.com
On Tue, Apr 15, 2014 at 2:10 PM, Francis Galiegue <fgal...@gmail.com> wrote:
> On Tue, Apr 15, 2014 at 1:37 PM, Gwyn Jensen <gwyn....@googlemail.com> wrote:
> [...]
>
>> if (report != null) {
>
> Not needed: report can never be null.
>

And while we are at fixing code...

Regexes in JSON Schema are _not_ anchored. So [A-Z]\d{4} will match
AI428983. You need to anchor it explicitly: ^[A-Z]\d{4}.

Gwyn Jensen

unread,
Apr 15, 2014, 8:36:47 AM4/15/14
to json-schem...@googlegroups.com, Gwyn Jensen
Unfortunately no.
Maven dependency:tree shows v1.7R of rhino brought in by json-schema-validator only.

Full stack trace below ...

com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: java.lang.ExceptionInInitializerError
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197)
at com.google.common.cache.LocalCache.get(LocalCache.java:3932)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3936)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4806)
at com.github.fge.jsonschema.core.processing.CachingProcessor.process(CachingProcessor.java:109)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:84)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:49)
at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:79)
at com.github.fge.jsonschema.main.JsonSchema.doValidate(JsonSchema.java:76)
at com.github.fge.jsonschema.main.JsonSchema.validate(JsonSchema.java:109)
at com.github.fge.jsonschema.main.JsonSchema.validate(JsonSchema.java:125)
at uk.gov.hmrc.restapi.emp.presentation.controller.EmployerControllerMVCTest.test(EmployerControllerMVCTest.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.google.common.util.concurrent.ExecutionError: java.lang.ExceptionInInitializerError
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197)
at com.google.common.cache.LocalCache.get(LocalCache.java:3932)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3936)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4806)
at com.github.fge.jsonschema.core.processing.CachingProcessor.process(CachingProcessor.java:109)
at com.github.fge.jsonschema.processors.validation.ValidationChain.process(ValidationChain.java:107)
at com.github.fge.jsonschema.processors.validation.ValidationChain.process(ValidationChain.java:56)
at com.github.fge.jsonschema.core.processing.ProcessorMap$Mapper.process(ProcessorMap.java:166)
at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:79)
at com.github.fge.jsonschema.core.processing.CachingProcessor$1.load(CachingProcessor.java:128)
at com.github.fge.jsonschema.core.processing.CachingProcessor$1.load(CachingProcessor.java:120)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3522)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2315)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2278)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2193)
... 41 more
Caused by: java.lang.ExceptionInInitializerError
at com.github.fge.jsonschema.core.keyword.syntax.checkers.common.PatternSyntaxChecker.checkValue(PatternSyntaxChecker.java:62)
at com.github.fge.jsonschema.core.keyword.syntax.checkers.AbstractSyntaxChecker.checkSyntax(AbstractSyntaxChecker.java:116)
at com.github.fge.jsonschema.core.keyword.syntax.SyntaxProcessor.validate(SyntaxProcessor.java:112)
at com.github.fge.jsonschema.core.keyword.syntax.SyntaxProcessor.validate(SyntaxProcessor.java:118)
at com.github.fge.jsonschema.core.keyword.syntax.SyntaxProcessor.validate(SyntaxProcessor.java:118)
at com.github.fge.jsonschema.core.keyword.syntax.SyntaxProcessor.rawProcess(SyntaxProcessor.java:70)
at com.github.fge.jsonschema.core.keyword.syntax.SyntaxProcessor.rawProcess(SyntaxProcessor.java:45)
at com.github.fge.jsonschema.core.processing.RawProcessor.process(RawProcessor.java:77)
at com.github.fge.jsonschema.core.processing.RawProcessor.process(RawProcessor.java:41)
at com.github.fge.jsonschema.core.processing.ProcessorChain$ProcessorMerger.process(ProcessorChain.java:190)
at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:79)
at com.github.fge.jsonschema.core.processing.CachingProcessor$1.load(CachingProcessor.java:128)
at com.github.fge.jsonschema.core.processing.CachingProcessor$1.load(CachingProcessor.java:120)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3522)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2315)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2278)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2193)
... 55 more
Caused by: org.mozilla.javascript.EvaluatorException: Cannot add a property to a sealed object.
at org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultErrorReporter.java:61)
at org.mozilla.javascript.Context.reportRuntimeError(Context.java:549)
at org.mozilla.javascript.Context.reportRuntimeError(Context.java:587)
at org.mozilla.javascript.Context.reportRuntimeError0(Context.java:557)
at org.mozilla.javascript.ScriptableObject.addSlot(ScriptableObject.java:1660)
at org.mozilla.javascript.ScriptableObject.getSlotToSet(ScriptableObject.java:1627)
at org.mozilla.javascript.ScriptableObject.put(ScriptableObject.java:223)
at org.mozilla.javascript.IdScriptable.put(IdScriptable.java:150)
at org.mozilla.javascript.ScriptableObject.defineProperty(ScriptableObject.java:1036)
at org.mozilla.javascript.IdScriptable.defineProperty(IdScriptable.java:233)
at org.mozilla.javascript.ScriptableObject.defineProperty(ScriptableObject.java:1057)
at org.mozilla.javascript.IdScriptable.addIdFunctionProperty(IdScriptable.java:451)
at org.mozilla.javascript.NativeString.fillConstructorProperties(NativeString.java:81)
at org.mozilla.javascript.IdScriptable.addAsPrototype(IdScriptable.java:424)
at org.mozilla.javascript.NativeString.init(NativeString.java:60)
at org.mozilla.javascript.Context.initStandardObjects(Context.java:656)
at com.github.fge.jsonschema.core.util.RhinoHelper.<clinit>(RhinoHelper.java:92)
... 72 more

Francis Galiegue

unread,
Apr 15, 2014, 12:41:06 PM4/15/14
to json-schem...@googlegroups.com
On Tue, Apr 15, 2014 at 2:36 PM, Gwyn Jensen <gwyn....@googlemail.com> wrote:
> Unfortunately no.
> Maven dependency:tree shows v1.7R of rhino brought in by
> json-schema-validator only.
>
> Full stack trace below ...
>

OK, the interesting part is this one:

[...]
This means the error happens in the static initializer of the RhinoHelper class:

static {
final Context ctx = Context.enter();
try {
SCOPE = ctx.initStandardObjects(null, true); // <-- HERE
ctx.evaluateString(SCOPE, jsAsString, "re", 1, null);
REGEX_IS_VALID = (Function) SCOPE.get("regexIsValid", SCOPE);
REG_MATCH = (Function) SCOPE.get("regMatch", SCOPE);
} finally {
Context.exit();
}
}

The problem I have is that after some research, I don't see that
people having encountered this exception so far are in quite the same
context... They try and override a method from the JavaScript standard
library but I do not (first link:
http://forums.pentaho.com/showthread.php?57264-JavaScript-Cannot-add-a-property-to-a-sealed-object).

I'll ask on Stack Overflow...

Francis Galiegue

unread,
Apr 15, 2014, 1:01:42 PM4/15/14
to Francis Galiegue, json-schem...@googlegroups.com
On Tue, Apr 15, 2014 at 6:41 PM, Francis Galiegue <fgal...@gmail.com> wrote:
[...]
>
> I'll ask on Stack Overflow...
>

In the meanwhile, could you describe your environment a little more?
What dependencies you use etc?

And also what version of json-schema-validator this is; the code of
this particular class hasn't changed for a while.

Gwyn Jensen

unread,
Apr 17, 2014, 5:39:42 AM4/17/14
to json-schem...@googlegroups.com
I would still appreciate some help with this if you have the time.
It does not appear to be anything I have done in either the schema or my use of your json-schema-validator code.
I get the same error with v2.0.3 as well.
There don.t appear to be any classpath clashes.
The exception occurs in the call to ...
ProcessingReport report = schema.validate(data);

Gwyn

Francis Galiegue

unread,
Apr 17, 2014, 6:52:33 AM4/17/14
to Gwyn Jensen, json-schem...@googlegroups.com
[Cc: group]

Hello,

On Thu, Apr 17, 2014 at 12:13 PM, Gwyn Jensen
<gwyn....@googlemail.com> wrote:
> Francis
>
> I have found and fixed the cause of the error and thought I'd share it with
> you in case any one else comes asking for help with the same problem.
> The problem was with the version of guava I was using. I was still on v15.0
> and it appears json-schema-validator needs the latest v16.0.1.
> I assume that in most cases it would get pulled in automatically as an
> inherited dependency but our artifactory was not pulling it in for some
> reason, and as I already had an explicit dependency on v15 it wasn't
> complaining.
> Maybe my explicit dependency was overriding the version needed for your
> library.
>
> Either way I have changed my dependency to require v16.0.1 and everything
> now works happily.
> Thanks
> Gwyn
>

Uh, now that is strange... I fail to see how Guava uses Rhino at all
:/ It has no dependency on it.

I'll try and browse their source to see...
Reply all
Reply to author
Forward
0 new messages