Skip to content
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

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

Closed
chris-dekker opened this issue May 20, 2022 · 11 comments

Comments

@chris-dekker
Copy link

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
@jamesdbloom
Copy link
Collaborator

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
Copy link
Collaborator

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

@Adm28
Copy link

Adm28 commented Jun 3, 2022

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 ?

@ghost
Copy link

ghost commented Jul 20, 2022

Same problem, templating is a great feature 😢

@jamesdbloom
Copy link
Collaborator

@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
Copy link
Collaborator

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

@gsuero
Copy link

gsuero commented Aug 19, 2022

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.

@ricardorqr
Copy link

Any update on this ticket?

@owain-roberts
Copy link

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

@vbutrim
Copy link

vbutrim commented Jan 30, 2023

It makes impossible right now for M1 users to use the newer versions of mockserver (#819), since this bug is not resolved :(

@Alexander-Khamitski
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants