Skip to content

Add x-codeSamples to generated OpenAPI #4371

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

miguelgrinberg
Copy link
Contributor

NOTE: this is not only a proof of concept but also a work in progress. DO NOT MERGE!

This change adds the x-codeSamples extension to the endpoints that have at least one example. A Console listing is added using the standard Kibana DevConsole syntax. Not implemented yet, but the idea is to run this DevConsole example through the request converter to generate versions of the example in all the supported languages.

@miguelgrinberg miguelgrinberg requested a review from a team as a code owner May 14, 2025 18:17
Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This will vastly improve the docs experience.

I'm sure the many if let Some can be improved, but at least the code is readable. Left two questions.

let mut code_samples = vec![];
if let Some((_, first_example)) = request_examples.clone().first() {
if let Some(example) = first_example.as_item() {
if let Some(description) = example.description.clone() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use summary when description is not set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature does not need summaries or descriptions, all I'm doing here is parsing the description text with the hope that somewhere within it I can find the method and URL of the request. The only reason I'm using description is because that is how these examples are parsed.

The proper thing to do would be to expand these examples to include this information in a easier format, at which point there wouldn't be a need to use descriptions or summaries for anything.

Comment on lines +265 to +273
if r.starts_with("GET ")
|| r.starts_with("POST ")
|| r.starts_with("PUT ")
|| r.starts_with("DELETE ")
|| r.starts_with("HEAD ")
{
request = Some(String::from(r));
break;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have examples that don't start with a method? QUERY will be a thing soon!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really a hack. I hope we don't have to do this. The problem is that the examples that were added to this repository are not clean Dev Console examples, so I'm forced to reconstruct the DevConsole syntax.

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

Successfully merging this pull request may close these issues.

2 participants