-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Docker errors using javascript template #1771
Comments
I have been forced to migrate to javascript templating because #1773 and now I face this issue as well when I tried to upgrade the version. |
seems to be related to #1326 (comment) |
I ran into this exact same problem. |
I've managed to get it working with the following dockerfile, building nashorn, renaming all the classes into the old namespace with jarjar, and copying the jars over into the mockserver libs directory: FROM openjdk:12-alpine as jarjar-build
RUN apk add git
RUN mkdir -p /build
WORKDIR /build
RUN git clone --depth 1 --branch fix-jdk11 https://github.com/redcape/jarjar
WORKDIR /build/jarjar
RUN ./gradlew jar
FROM alpine as nashorn-build
RUN apk add git openjdk17 apache-ant
RUN mkdir -p /build/
WORKDIR /build/
RUN git clone --depth 1 https://github.com/openjdk/nashorn
WORKDIR /build/nashorn/make/nashorn
RUN ant jar
COPY --from=jarjar-build /build/jarjar/jarjar-command/build/libs/jarjar-command-1.1.1.jar ./jarjar.jar
COPY nashorn.rename.rules .
RUN java -jar jarjar.jar /build/nashorn/build/nashorn/dist/nashorn.jar --rules ./nashorn.rename.rules --output /build/nashorn/build/nashorn/dist/nashorn.renamed.jar
FROM mockserver/mockserver
COPY --from=nashorn-build /build/nashorn/build/nashorn/dist/nashorn.renamed.jar /libs/nashorn.renamed.jar
COPY --from=nashorn-build /build/nashorn/build/nashorn/dependencies/*.jar /libs/.
ARG MOCK_FILE
COPY ${MOCK_FILE} /mockdata/${MOCK_FILE}
ENV MOCKSERVER_INITIALIZATION_JSON_PATH=/mockdata/${MOCK_FILE} nashorn.rename.rules:
Of particular note, the main branch of jarjar doesn't work, you need to use the fork from shevek/jarjar#23 for that fix, otherwise jarjar won't rename all the classes properly. (The last 3 lines of the dockerfile aren't relevant to the issue and can be removed) |
I am trying this docker file, getting: Failed generating bytecode for :3 any idea? |
This and any other string-based references to class paths is my guess. I doubt jarjar will be modifying any strings like that, so short of patching the nashorn code directly I don't really see a workaround with this jarjar renaming method. My use case is admittedly simple right now: I'm essentially just returning a response that references a single field in the request, so it seems it's not a suitable workaround for anything much more complex than that. EDIT: "httpResponseTemplate": {
"templateType": "JAVASCRIPT",
"template": "const matches = JSON.parse(request.body).signup.email.match(/^(\\d+).([^@]+)@test/); return { statusCode: parseInt(matches[1]), headers: { 'access-control-allow-origin': '*', 'access-control-allow-methods': 'POST' }, body: { type: 'JSON', json: { error: { code: matches[2] } } } }"
} |
OK, got it, thank you |
HI, ok so I manually created a version of nashorn with the relevant packages changes and added to the mockserver Caused by: :3 SyntaxError: Invalid JSON: :1:0 Expected json literal but found u this the template I am using: req = JSON.parse(request.body.json).data[0].siteSyncRecord; return {statusCode:200,headers:{'Content-Type':['application/json']},body:{data:[{siteSyncRecord:{externalId:req.externalId,accountId:req.accountId,displayName:req.displayName,internalId:req.externalId-3252}}]}} and the request is: { full stacktrace: at org.mockserver.templates.engine.javascript.JavaScriptTemplateEngine.executeTemplate(JavaScriptTemplateEngine.java:109)
|
Hi, I manage to overcome the issue above, I had to update the json template. my request structure is: `{ "body": { the original template (that was working in version 5.5.4:
the updated one:
|
Hi! I have the same issue for the following expectation: {
"httpRequest": {
"method": "GET",
"path": "/some/click"
},
"httpResponseTemplate": {
"template": "return { statusCode: 301, headers: { 'Location': [ 'http://domain.test?token=' + (request.queryStringParameters['token'] && request.queryStringParameters['token'][0]) ] } };",
"templateType": "JAVASCRIPT"
}
} With the same error in logs
|
Hi! I also have the same issue, with the exact same stack trace. For now I just had to switch to using the velocity templates and they seem to be stable with the latest docker image (5.15.0). Do we know when this may be resolved? |
Describe the issue
Lots of errors in the logs as per below.
I believe the cause is use of JDK 17 which does not have nashorn.
What you are trying to do
Description of what you are trying to do so we can understand the context of the problem
MockServer version
5.15.0 Docker
To Reproduce
Anything that uses the templateType JAVASCRIPT e.g.
How you are running MockServer (i.e maven plugin, docker, etc)
Docker
Code you used to create expectations
As Above
What error you saw
As above
Expected behaviour
No errors
MockServer Log
As above
The text was updated successfully, but these errors were encountered: