Skip to content

add Java language examples #4580

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

Merged
merged 32 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0a4dbd6
calling java converter
l-trotta Jun 16, 2025
ec87137
pushing to same file as other examples
l-trotta Jun 16, 2025
f097ec1
fix java output
l-trotta Jun 16, 2025
6f66224
java example in model
l-trotta Jun 16, 2025
a7e5b52
update request converter
l-trotta Jun 16, 2025
79e237d
generate
l-trotta Jun 16, 2025
e2ff2c8
specific java examples command
l-trotta Jun 16, 2025
9423866
a
l-trotta Jun 16, 2025
2281a5e
more java example up
l-trotta Jun 16, 2025
828d50c
a
l-trotta Jun 16, 2025
e76a1e5
back down
l-trotta Jun 16, 2025
4e35901
back up
l-trotta Jun 16, 2025
8003572
manual fixes
l-trotta Jun 16, 2025
ee8bb29
schema json
l-trotta Jun 16, 2025
2668b6f
update openapi generator
l-trotta Jun 16, 2025
b9a484e
update metamodel
l-trotta Jun 16, 2025
4829efe
openapi output
l-trotta Jun 16, 2025
c8fea03
fix rebase
l-trotta Jun 17, 2025
673283f
remove alternative_java examples
l-trotta Jun 17, 2025
3dd1512
fix generate script
l-trotta Jun 17, 2025
5243c0c
regen examples under alternatives field
l-trotta Jun 17, 2025
c4f6ff4
reapply manual fixes
l-trotta Jun 17, 2025
2dd8df6
full output regen
l-trotta Jun 17, 2025
9291df4
remove leftover code
l-trotta Jun 17, 2025
c626c6b
remove old, remove reindex
l-trotta Jun 18, 2025
9d1e095
capitalize java
l-trotta Jun 18, 2025
748611f
updated java examples with spaces instead of tabs
l-trotta Jun 18, 2025
e515817
rereapply manual fixes
l-trotta Jun 18, 2025
93355bd
output regen
l-trotta Jun 18, 2025
2fc7025
restore first reindex example, regen
l-trotta Jun 18, 2025
e8399f8
fix more tabs, regen
l-trotta Jun 18, 2025
4c7e630
Merge branch 'main' into java-examples
l-trotta Jun 18, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ generate-language-examples:
@node docs/examples/generate-language-examples.js
@npm run format:fix-examples --prefix compiler

generate-language-examples-with-java:
@node docs/examples/generate-language-examples.js java
@npm run format:fix-examples --prefix compiler

lint-docs: ## Lint the OpenAPI documents after overlays
@npx @redocly/cli lint "output/openapi/elasticsearch-*.json" --config "docs/linters/redocly.yaml" --format stylish --max-problems 500

Expand Down
Binary file modified compiler-rs/compiler-wasm-lib/pkg/compiler_wasm_lib_bg.wasm
Binary file not shown.
63 changes: 63 additions & 0 deletions docs/examples/generate-language-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const fs = require('fs');
const path = require('path');
const { parseDocument: yamlParseDocument } = require('yaml');
const { convertRequests, loadSchema } = require('@elastic/request-converter');
const {parseRequest} = require("@elastic/request-converter/dist/parse");
const {JavaCaller} = require("java-caller");

const LANGUAGES = ['Python', 'JavaScript', 'Ruby', 'PHP', 'curl'];

Expand All @@ -44,6 +46,50 @@ async function generateLanguages(example) {
});
}
data.alternatives = alternatives.concat(data.alternatives.filter(pair => !LANGUAGES.includes(pair.language)));

// specific java example generator
if (process.argv[2] === "java") {
const partialRequest = await parseRequest(request);
const java = new JavaCaller({
minimumJavaVersion: 21,
jar: "path/to/converter/jar/java-es-request-converter-1.0-SNAPSHOT.jar",
});

let correctParams = getCodeGenParamNames(partialRequest.params, partialRequest.request);
let body = partialRequest.body;
if (!body) {
body = {}
}

let javaReqs = [];
const javaParsedRequest = {
api: partialRequest.api,
params: correctParams,
query: partialRequest.query,
body: body,
};
javaReqs.push(javaParsedRequest)

let args = [];
args.push(JSON.stringify(javaReqs));

const {status, stdout, stderr} = await java.run(args);
if (status) {
console.log(stderr);
console.log(JSON.stringify(javaReqs));
}
else {
const alternative_java = [];
alternative_java.push({
language: "Java",
code: stdout,
});
// replace old java examples
data.alternatives = data.alternatives.filter(pair => pair.language !== "Java");
data.alternatives = data.alternatives.concat(alternative_java);
}
}

doc.delete('alternatives');
doc.add(doc.createPair('alternatives', data.alternatives));
await fs.promises.writeFile(example, doc.toString({lineWidth: 132}));
Expand All @@ -61,6 +107,23 @@ async function* walkExamples(dir) {
}
}

function getCodeGenParamNames(
params,
request,
){
for (const [key, value] of Object.entries(params)) {
if (request?.path) {
for (const prop of request.path) {
if (prop.name === key && prop.codegenName !== undefined) {
delete params[key];
params[prop.codegenName] = value;
}
}
}
}
return params;
}

async function main() {
let count = 0;
let errors = 0;
Expand Down
152 changes: 152 additions & 0 deletions docs/examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@elastic/request-converter": "^9.1.1",
"@redocly/cli": "^1.34.3",
"@stoplight/spectral-cli": "^6.14.2",
"yaml": "^2.8.0"
"yaml": "^2.8.0",
"java-caller": "^4.1.1"
}
}
Loading
Loading