GenieFramework / Genie.jl

🧞The highly productive Julia web framework
https://genieframework.com
MIT License
2.28k stars 192 forks source link

integration with OpenAPI #662

Open rssdev10 opened 1 year ago

rssdev10 commented 1 year ago

OpenAPI is a common way to describe API specification both for server and clients. The package https://github.com/JuliaComputing/OpenAPI.jl as well as https://openapi-generator.tech/docs/generators/julia-server/ provides a server code generation with data structures both for requests and responses. And, the code provides data parsing, validation and serializing of the results.

But OpenAPI.jl doesn't provide any functionality for authentication, CORS, routing and logging. These parts of the code should be implemented manually. See the example - https://github.com/OpenSesame/GptSearchPlugin/blob/main/src/server.jl or https://github.com/JuliaComputing/OpenAPI.jl/blob/main/test/server/petstore_v3/petstore_server.jl

It would be good to have a way to reuse components of Genie to have an easy way to write web services based on external OpenAPI specification.

essenciary commented 1 year ago

That's a great idea, thanks! Are you familiar with the OpenAPI.jl package (as I'm not). Could you help with this?

rssdev10 commented 1 year ago

Hi, yes, we are working with OpenAPI.jl.

E.g. there is the specification - https://github.com/OpenSesame/GptSearchPlugin/blob/main/resources/dev-openai/.well-known/openapi.yaml

openapi-generator can be installed by different ways - https://openapi-generator.tech/docs/installation/ or be used directly as a openapi-generator-cli java-jar file from https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/.

And, there is a script for generating the code: https://github.com/OpenSesame/GptSearchPlugin/blob/main/scripts/openapi_generator.sh

For this generated code some additional methods should be implemented. E.g. in our sample, these methods are mentioned here - https://github.com/OpenSesame/GptSearchPlugin/blob/main/src/generated/src/GptPluginServer.jl#L8-L16

And, the routes should be registered: https://github.com/OpenSesame/GptSearchPlugin/blob/main/src/generated/src/GptPluginServer.jl#L32-L51

As a result, we created https://github.com/OpenSesame/GptSearchPlugin/blob/main/src/server.jl

The service entry point in our case is https://github.com/OpenSesame/GptSearchPlugin/blob/main/ws_run.jl

More general examples are available at https://github.com/JuliaComputing/OpenAPI.jl/tree/main/test/server And I think @tanmaykm, who is the author of OpenAPI.jl, can also comment on it.

essenciary commented 1 year ago

Thank you for the details @rssdev10 - very helpful!

rssdev10 commented 1 year ago

Actually I see two scenarios here:

  1. Keep the generated with OpenAPI code as is. And implement a service with components based on Genie (routing, security, logging).
  2. Implement new templates for code generation based on the code https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/julia-server . But in this case the question will be - how to keep consistent the Julia code in general for generating and validating data structures. It would be really good to have the only version of this code.
tanmaykm commented 1 year ago

Thanks for initiating this. It will be great to have OpenAPI and Genie work together. I am yet to go through the details, but am happy to pitch in wherever needed.