mockserver icon indicating copy to clipboard operation
mockserver copied to clipboard

JavaScript templating broken in docker after Java 17 upgrade: Missing nashorn

Open chris-dekker opened this issue 2 years ago • 4 comments

Describe the issue Javascript-templated expectations seem to no longer work after the upgrade to Java 17 in 5.13.1, due to the Nashorn JS engine no longer being included in the JVM.

Normally I would be able to use a different JVM, but for the pre-built docker images I think these should be included to work out of the box!

What you are trying to do Setting up Javascript-templated expectations to evaluate the request at runtime.

MockServer version 5.13.2

To Reproduce Steps to reproduce the issue:

Run Mockserver using docker-compose directly from published images from docker hub:

  mock-server:
    image: jamesdbloom/mockserver:mockserver-5.13.2
    command: >-
      -logLevel INFO
      -serverPort 1080
    ports:
      - '1080:1080'

Create a request using JS templating:

  {
    "httpRequest": {
      "method": "POST",
      "path": "/api/my/url"
    },
    "httpResponseTemplate": {
      "templateType": "JAVASCRIPT",
      "template": "..."
    }
  }

Call the expected endpoint. Notice a 404 response and the error in the mockserver docker compose log. See below.

Expected behaviour JS templated response to be evaluated and returned without errors.

MockServer Log

2022-05-20 09:49:38 5.13.2 SEVERE 1080 JavaScript based templating is only available in a JVM with the "nashorn" JavaScript engine, please use a JVM with the "nashorn" JavaScript engine, such as Oracle Java 8+

  java.lang.RuntimeException: "nashorn" JavaScript engine not available

chris-dekker avatar May 20 '22 11:05 chris-dekker

There could certainly be multiple versions, one with an older JVM version and the default with a later version. The reason I say that is that the later Java versions have better handling of memory and fix some JDK bugs with TLS, etc.

jamesdbloom avatar May 20 '22 14:05 jamesdbloom

I do agree however that it would make sense to have a docker container version that supported JS templating.

jamesdbloom avatar May 20 '22 14:05 jamesdbloom

I get the following exception for httptemplate responses in 5.12.0 and 5.13.0

at org.mockserver.templates.engine.javascript.JavaScriptTemplateEngine.executeTemplate(JavaScriptTemplateEngine.java:96)
	at org.mockserver.mock.action.http.HttpResponseTemplateActionHandler.handle(HttpResponseTemplateActionHandler.java:42)
	at org.mockserver.mock.action.http.HttpActionHandler.lambda$null$3(HttpActionHandler.java:111)
	at org.mockserver.mock.action.http.HttpActionHandler.handleAnyException(HttpActionHandler.java:339)
	at org.mockserver.mock.action.http.HttpActionHandler.lambda$processAction$4(HttpActionHandler.java:110)
	at org.mockserver.scheduler.Scheduler.run(Scheduler.java:87)
	at org.mockserver.scheduler.Scheduler.schedule(Scheduler.java:113)
	at org.mockserver.mock.action.http.HttpActionHandler.processAction(HttpActionHandler.java:110)
	at org.mockserver.netty.HttpRequestHandler.channelRead0(HttpRequestHandler.java:166)
	at org.mockserver.netty.HttpRequestHandler.channelRead0(HttpRequestHandler.java:50)
	at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
	at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
	at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at org.mockserver.dashboard.DashboardWebSocketHandler.channelRead(DashboardWebSocketHandler.java:137)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at org.mockserver.netty.websocketregistry.CallbackWebSocketServerHandler.channelRead(CallbackWebSocketServerHandler.java:57)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
	at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:327)
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:299)
	at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at org.mockserver.netty.unification.PortUnificationHandler.switchToHttp(PortUnificationHandler.java:260)
	at org.mockserver.netty.unification.PortUnificationHandler.decode(PortUnificationHandler.java:138)
	at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:510)
	at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366)
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:279)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:722)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496)
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: <eval>:3 SyntaxError: Invalid JSON: <json>:1:0 Expected json literal but found u
undefined
^
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.ECMAErrors.syntaxError(ECMAErrors.java:160)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.ECMAErrors.syntaxError(ECMAErrors.java:145)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.JSONFunctions.parse(JSONFunctions.java:81)
	at jdk.scripting.nashorn/jdk.nashorn.internal.objects.NativeJSON.parse(NativeJSON.java:118)
	at jdk.scripting.nashorn.scripts/jdk.nashorn.internal.scripts.Script$Recompilation$5$\^eval\_$cu1$restOf/0x0000000100431040.handle(<eval>:3)
	at jdk.scripting.nashorn.scripts/jdk.nashorn.internal.scripts.Script$Recompilation$3$166A$\^eval\_$cu1$restOf/0x0000000100419040.serialise(<eval>:4)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:657)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527)
	at jdk.scripting.nashorn/jdk.nashorn.api.scripting.ScriptObjectMirror.call(ScriptObjectMirror.java:120)
	at org.mockserver.templates.engine.javascript.JavaScriptTemplateEngine.executeTemplate(JavaScriptTemplateEngine.java:56)
	... 62 more
Caused by: jdk.nashorn.internal.runtime.ParserException: <json>:1:0 Expected json literal but found u
undefined
^
	at jdk.scripting.nashorn/jdk.nashorn.internal.parser.JSONParser.error(JSONParser.java:529)
	at jdk.scripting.nashorn/jdk.nashorn.internal.parser.JSONParser.error(JSONParser.java:533)
	at jdk.scripting.nashorn/jdk.nashorn.internal.parser.JSONParser.expectedError(JSONParser.java:541)
	at jdk.scripting.nashorn/jdk.nashorn.internal.parser.JSONParser.parseLiteral(JSONParser.java:182)
	at jdk.scripting.nashorn/jdk.nashorn.internal.parser.JSONParser.parse(JSONParser.java:148)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.JSONFunctions.parse(JSONFunctions.java:79)
	... 70 more

Is there any way to fix this or has someone taken an initiative from your side to fix this issue in the latest version ?

Adm28 avatar Jun 03 '22 06:06 Adm28

Same problem, templating is a great feature 😢

ghost avatar Jul 20 '22 14:07 ghost

@Adm28 there error looks like there is a bug in your template, do you know that isn't the case? Can you please provide your template (i.e. the expectation) and how you are submitting it so I can check where the issue is.

jamesdbloom avatar Aug 18 '22 07:08 jamesdbloom

I've created this backlog item: https://trello.com/c/MsmlhWJI/191-create-docker-image-with-support-for-js-templating-and-documentation so I'm going to close this issue and will aim to provide a docker container that supports templating and documentation of how to do this without the docker container

jamesdbloom avatar Aug 18 '22 07:08 jamesdbloom

I've created this backlog item: https://trello.com/c/MsmlhWJI/191-create-docker-image-with-support-for-js-templating-and-documentation so I'm going to close this issue and will aim to provide a docker container that supports templating and documentation of how to do this without the docker container

Looking forward for it.

gsuero avatar Aug 19 '22 00:08 gsuero

Any update on this ticket?

ricardorqr avatar Aug 26 '22 21:08 ricardorqr

I came across this issue also. My workaround at the moment was to use version 5.12.0.

owain-roberts avatar Sep 06 '22 12:09 owain-roberts

It makes impossible right now for M1 users to use the newer versions of mockserver (https://github.com/mock-server/mockserver/issues/819), since this bug is not resolved :(

vbutrim avatar Jan 30 '23 17:01 vbutrim

Faced this issue also. Rolled back to 5.12.0 and JS template type works fine!

Alexander-Khamitski avatar Feb 16 '24 10:02 Alexander-Khamitski