Skip to content
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

Generating Huma Code from OpenAPI Spec #439

Open
srilman opened this issue May 14, 2024 · 2 comments
Open

Generating Huma Code from OpenAPI Spec #439

srilman opened this issue May 14, 2024 · 2 comments
Labels
question Further information is requested

Comments

@srilman
Copy link

srilman commented May 14, 2024

Is there any way to generate a REST API using Huma from an OpenAPI spec? The docs mention the opposite, using Huma and oapi-generator to reconstruct a server without Huma. However, I have a use-case where I have an existing OpenAPI spec that I want to implement while retaining some of the other benefits of Huma (validation, docs generation, etc) without needing to copy everything over.

I wasn't sure whether to ask this question in this repo or the oapi-generator repo. I does seem like more their alley, but they may not store all of the spec info for codegen.

@danielgtaylor danielgtaylor added the question Further information is requested label May 14, 2024
@danielgtaylor
Copy link
Owner

@srilman thanks for the question! Unfortunately that isn't possible at the moment. Huma does not generate anything from OpenAPI specs itself. It would be possible to create a template for other code generators that spits out Go code using Huma, but I don't think that exists at the moment. As a side note, it's also a bit complicated because Huma supports most but not all of the OpenAPI features, and some OpenAPI documents can be written in a way that makes it hard to generate sane Go code (for example using oneOf/anyOf/not or complex branching, constants, etc).

Anyway I'd be happy if someone wants to build this, otherwise you may just have to manually convert it to Go code for the time being.

@srilman
Copy link
Author

srilman commented May 14, 2024

Good point about the complex spec features (oneOf, anyOf, etc), I didn't think about that. Thankfully, I can leverage another Go library that has some of these parts already implemented.

I think for the time being, I'm going to write a small Python script using https://github.com/Dorthu/openapi3 to generate a template, and then fill in the blanks for anything too complicated. Once I have something working, I'm happy to share a Gist.

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

No branches or pull requests

2 participants