-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Generate curl samples for requests #15
Comments
👍 |
Can we actually render |
Ahh ok thanks. |
Just wondering if there was any plans to move on this? This feature would be excellent. I'm not too familiar with the code base, but this seems a good workaround solution before coding up a full API console integration. |
I have a fully working code generator: |
That looks amazing, jgabriel. Can't wait to be able to use it! |
@ornicar: |
Thanks, I tried to use it but it doesn't seem to like my YAML file. I'll try something tomorrow. |
Is it possible to grab the code from swagger-ui? |
How do you generate the x-code-samples code snippets in the yaml/json file? Is there any way to do it automatically? |
This is what I use: It automatically generates code samples for a bunch of different languages. You can see what it looks like here: |
Thank you @jgabriels , but I see that swagger-snippet only works with version 2.0 of Open Api and I have a 3.0 file, is that correct? |
I believe that may be correct. One workaround might be to use one of the many 3.0 to 2.0 converters to make a 2.0 version of your api and run the code generator on that. |
Any updates on this? |
Any updates? |
I think this feature is overrated. A simple solution would be to add a "Open in Postman" link. I am not sure if this is possible. |
@SebastianStehle or simply import YAML to Insomnia, BUT it does not replace the mentioned functionality from this issue at all! People, like me, may not have Postman around them. If you are in console on the server where you should send the request from, it might take you quite a number of actions before you tunnel the request from your PC to the server and use your favorite UI to send requests through it. And last, but not least: nothing beats the simplicity of sending a request using curl regardless whether you are on Linux, BSD, Mac or Windows, just paste the query into console and press "Enter". |
It cannot be understated how important Curl support is for API documentation |
Any progress on this issue? This feature is essential yet still missing from ReDoc... |
please guide i am looking for the exact same but unable to get any lead is the feature still missing from ReDoc...... |
I tried swagger-codegen, openapi-generator and now landed here. It's the best choice in my opinion, but a CURL example support is the one feature I'm missing here. : / |
https://github.com/cdwv/oas3-api-snippet-enricher works for some cases. It looks for defaults/examples in a particular place so if you have them differently the snippets won't be very helpful. |
This package worked for me I hope it helps someone. |
Using the openapi-snippet mentioned above by hungtsou, the following (rough) script will add the
|
Here is an improved version of the script above, change const OpenAPISnippet = require('openapi-snippet')
const addRequestSamples = (swaggerJson) => {
const swagger = JSON.parse(JSON.stringify(swaggerJson));
const targets = ['shell_curl'];
const httpRequestMethods = ['get', 'head', 'post', 'put', 'delete', 'options', 'trace', 'patch'];
for (const singlePath in swagger.paths) {
Object.keys(swagger.paths[singlePath])
.filter((method) => httpRequestMethods.some((supportedMethod) => supportedMethod === method))
.forEach((method) => {
try {
const snippets = OpenAPISnippet.getEndpointSnippets(swagger, singlePath, method, targets);
const samples = [];
snippets.snippets.forEach((snippet) => {
samples.push({
lang: snippet.title.split(' ')[0],
source: snippet.content,
});
});
swagger.paths[singlePath][method]['x-codeSamples'] = samples;
} catch (error) {
console.log(error);
}
});
}
return swagger;
}; |
^^ the above answers combined const openAPISnippet = require('openapi-snippet');
const yaml = require('js-yaml');
const fs = require('fs');
const targets = ['node_fetch', 'python_requests', 'shell_curl'];
const httpRequestMethods = ['get', 'head', 'post', 'put', 'delete', 'options', 'trace', 'patch'];
const addRequestSamples = (swaggerJson) => {
const swagger = JSON.parse(JSON.stringify(swaggerJson));
for (const singlePath in swagger.paths) {
Object.keys(swagger.paths[singlePath])
.filter((method) => httpRequestMethods.some((supportedMethod) => supportedMethod === method))
.forEach((method) => {
try {
const snippets = openAPISnippet.getEndpointSnippets(
swagger,
singlePath,
method,
targets,
);
const samples = [];
snippets.snippets.forEach((snippet) => {
samples.push({
lang: snippet.title.split(' ')[0],
source: snippet.content,
});
});
swagger.paths[singlePath][method]['x-codeSamples'] = samples;
} catch (error) {
console.log(error);
}
});
}
return swagger;
};
const args = process.argv.slice(2);
const openApi = yaml.load(fs.readFileSync(args[0], 'utf8'));
console.log(yaml.dump(addRequestSamples(openApi))); |
No description provided.
The text was updated successfully, but these errors were encountered: