Skip to content

OpenAPI Export for API Definition, Swagger UI Binding#1454

Closed
Nanashi-lab wants to merge 17 commits intogolemcloud:mainfrom
Nanashi-lab:openapi
Closed

OpenAPI Export for API Definition, Swagger UI Binding#1454
Nanashi-lab wants to merge 17 commits intogolemcloud:mainfrom
Nanashi-lab:openapi

Conversation

@Nanashi-lab
Copy link
Copy Markdown
Contributor

@Nanashi-lab Nanashi-lab commented Mar 28, 2025

PR: OpenAPI Export and Swagger UI Support for Golem API Definitions Closes #1178
/claim #1178

This PR provides OpenAPI export functionality and Swagger UI (Binding and CLI) support. Specifically, this PR implements:

  • Key Features:
    • OpenAPI Export Endpoint: Added /v1/api-definitions/{id}/{version}/export endpoint to export API Definitions to OpenAPI Specifications.
    • Full and Lossless Conversion: Implemented conversion logic for request and response types, accurate mapping of type to OpenAPI schema.
    • Swagger UI Binding Type: A new "Swagger UI" binding type for routes in the Worker Gateway, allowing for serving of Swagger UI interfaces from API definitions when deployed.
    • Swagger UI Integration: Code to render the exported OpenAPI schema as a Swagger UI for user-friendly API testing.
    • Testing: Added basic unit for API to OpenAPI spec, tests include, verifying CORS, secuirty, parameter, input and output maping, I have also added end-to-end tests to validate Swagger UI.
    • Golem CLI Updates: golem-cli needs to be updated with:
      • golem-cli api-definition swagger subcommand for launching a browser to a generated Swagger UI.
      • golem-cli api-definition export subcommand to export an API definition to an OpenAPI schema.
    • Documentation: Documentation is required to explain these new features and will be provided in a separate PR to the docs repository.
    • System/Integration Tests:
      • System tests to OpenAPI Spec from API, and attempting full circle by importing the OpenApi Schema,

System test and verify they interact correctly with the SWAGGER UI (Might not be possible currently all deployment tests fail.)

You can try this out, after building, by creating a new route with bindingtype swagger-ui, with path without any varriables, example /swagger . If deployed to localhost:9006, you will find your swagger ui there.

Your feedback is appreciated! While I work on the remaining task.

Capture
Capture2

@Nanashi-lab Nanashi-lab changed the title OpenAPI Export for API Definition, Swagger UI Binding - Closes #1234 OpenAPI Export for API Definition, Swagger UI Binding - Closes #1178 Mar 28, 2025
@Nanashi-lab Nanashi-lab reopened this Mar 28, 2025
@Nanashi-lab Nanashi-lab marked this pull request as ready for review March 28, 2025 04:21
@Nanashi-lab
Copy link
Copy Markdown
Contributor Author

Nanashi-lab commented Mar 28, 2025

PR for CLI Part of the task is here
There are two new command under golem-cli api definition
swagger and export

golem-cli api definition export --id <id> --version <version> will download the open api spec in either json or yaml format, this will be the whole open api schema including input and output type

export

golem-cli api definition export --id <id> --version <version> --host <host> will open the webbrowser and navigate to the host (usually localhost:), if the api has deployments it will add the deployments in server, to test. else it will show the open api without any server information

swagger

example of the swagger ui usage in localhost:9900, it rightly shows 9006 (local deployment) as server for testing, it also doesnt show cors-binding or swagger ui binding. It only shows binding-type -> default

Capture3

@Nanashi-lab
Copy link
Copy Markdown
Contributor Author

This is a link to repository containing, some wasm-files, api json files and the equivalent yaml output files.

@Nanashi-lab
Copy link
Copy Markdown
Contributor Author

All Checks have passed, that's really good news.

@Nanashi-lab
Copy link
Copy Markdown
Contributor Author

I matched HEAD with current golem,
Few other changes are

  1. Removed extract_path_parameters, we get path details directly input_mapping
  2. Added test for first class worker
  3. Added test for variant and query
  4. Cleaned up the repeated test.

@Nanashi-lab Nanashi-lab changed the title OpenAPI Export for API Definition, Swagger UI Binding - Closes #1178 OpenAPI Export for API Definition, Swagger UI Binding Apr 12, 2025
@Nanashi-lab Nanashi-lab requested a review from jdegoes April 23, 2025 00:19
@Nanashi-lab
Copy link
Copy Markdown
Contributor Author

Nanashi-lab commented Apr 23, 2025

One of my tests failed

ERROR test_export_import_api_definition{deps=EnvBasedTestDependencies}: golem_common::tracing: panic panic_info=panicked at integration-tests/tests/api/api_definition.rs:587:10:
called `Result::unwrap()` on an `Err` value: ApiDefinitionError { error: Some(BadRequest(ErrorsBody { errors: ["Invalid JSON"] })) }"

I will figure out why this happening. Couldnt replicate this locally. I split the test into one simple test for export endpoint and another for roundtrip test

@Nanashi-lab
Copy link
Copy Markdown
Contributor Author

Nanashi-lab commented Apr 30, 2025

@jdegoes @vigoo Gentle Reminder -

Please review & offer feedback on this PR, which I submitted about a month ago. To ensure it's as easy as possible to review, I've rebased both golem and golem-cli with the latest main branch, so it is up-to-date.

Could you also let me know the current state of the ticket? Is the PR/Issue still something the team is actively interested in? Knowing this will help me prioritize accordingly.

I want to ensure I'm providing everything you need to make a decision on this PR. Please let me know what I can do to help move it forward.

To make this process smoother, I'm happy to:

  • Set up a live demo to walk you through the changes.
  • Provide any additional technical information or documentation you need.
  • Answer any questions you may have.

P.S. - The ignored roundtrip should be possible after #1569

@vigoo
Copy link
Copy Markdown
Contributor

vigoo commented Apr 30, 2025

Is the PR/Issue still something the team is actively interested in?

Yes we definitely want this feature!

@afsalthaj
Copy link
Copy Markdown
Contributor

@Nanashi-lab I am currently taking a look at this PR

Clean testframework, and swagger-ui handler
Remove string scanning in api_oas_convert.rs
Change add new more comprehensive test in api_oas_convert_tests
Cargo fix
Test for default response
Remove extract_path_parameters
Add support for query parameters
Add test for first class workers
Add test for query parameters and variant
Cargo make fix
Add Getters for Component Name and Version
Change ExportApiDefinition to ExportOpenapiSpec
@afsalthaj
Copy link
Copy Markdown
Contributor

@Nanashi-lab I would request you to commit the fixes after I finish the review - I will let you know when, otherwise hard for me to review :D

It's looking good already, but just need a couple of more hours.

}

// Fallback to default object schema if no body is found
create_default_object_schema()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I hope this is not by any accident :)
What is called as default_object_schema? That it expects empty object in the request body?
Couldn't we completely avoid a schema if there is none in the request.body?

))),
..Default::default()
};
content.insert("application/json".to_string(), media);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think it is application/json all the time

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We do support application/text as well, and you can try it out as well.
If our rib script is as follows, and then deploy it by changing the content types

        - method: GET
          path: /v1/{user}/contents
          binding:
            type: default
            componentName: "amazon:shopping-cart"
            response: |
              let user: u32 = request.path.user;
              let worker = instance("cart-${user}");
              {headers: { Content-Type: "application/text" }, body: worker.get-string()}
        

The worker function merely returns String.

curl -X GET http://localhost:9006/v1/100/contents                                                                            
this is a string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

And if it's json

  {headers: { Content-Type: "application/json" }, body: worker.get-string()}

then,

curl -X GET http://localhost:9006/v1/100/contents                                                                            
"this is a string"

Copy link
Copy Markdown
Contributor

@afsalthaj afsalthaj left a comment

Choose a reason for hiding this comment

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

I have made some comments. There are a few things to fix before we could merge. I can see you are getting into various internals to reach this point, and pretty sure we can make it all the way if you could fix these comments soon :)

@jdegoes
Copy link
Copy Markdown
Contributor

jdegoes commented May 22, 2025

@Nanashi-lab Please re-open when 100% of all feedback has been addressed in the way indicated and you are ready for a fresh review. Thank you!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Develop OpenAPI Export for API Definition

4 participants