Skip to content

Add openAPI client generation - #71

Open
davidn wants to merge 9 commits into
gofoji:mainfrom
davidn:openapi-client
Open

Add openAPI client generation#71
davidn wants to merge 9 commits into
gofoji:mainfrom
davidn:openapi-client

Conversation

@davidn

@davidn davidn commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Motiation: writing HTTP clients from an openapi spec is tedious and mechanistic, especially when this effort is being repeated for multiple callers of an API. While other code generators exist, they do not follow the idiomatic style of foji and its users.

What this PR does: Adds a template to generate a HTTP client library with the following characteristics (aiming to mirror handler.go.tpl):

  1. A NewClient method that takes in authentication functions, akin to RegisterHTTP. The actual authentication functions are a mirror of those used in the handler: they take a user object and return the require token/username/password/cookie etc.
  2. One method per operation, with the same signature as the handler operations interface methods. These handle JSON encoding/decoding, constructing the request, applying authorization, checking for non-http status codes. The do no validation beyond what is part of the json decoding.

Note, the client template in this PR is LLM-generated. I guided it by closely specifying and reviewing the client code it outputted.

For an example of how code can use these generated clients, see https://gist.github.com/davidn/8b5ed70a4704e1dcb0f9c63159632ef8

davidn added 4 commits July 6, 2026 11:11
client.go.tpl and the changes to output/openapi.go in this commit are
largely made by an LLM. I added example cases and have closely reviewed
the outputted client code. I therefore vouch more for the quality of
output than quality of code.

This commit is a WIP, I want to do the following:

1. reduce changes to openapi.go
2. Add more helpers to iken and use those in the client
This commit changes the generated client library to use a (not yet
merged) iken change that adds an standard error and several auth types
akin to those used in handler.go.tpl.

This has the effect of:

1) Allowing clients to use a single error.As that works across any
   client.

2) Allowing clients to pass a user object in the API methods, with
   helpers that translate the user object into the actual wire
   credential.  This matches the handler.go.tpl style. See
   https://gist.github.com/davidn/8b5ed70a4704e1dcb0f9c63159632ef8 for
   an example of how this gets used.
Motivation: OpenAPI operations may specify several security
requirements, each of which consists of several security schemas. A
invocation must fulfil all the security schemas of at least one security
requirement. In addition, it's not possible to have a "no op"
authenticator as even a empty token/cookie/user/pass would get encoded
into the request.

Faced with having to explicitly handle authenticators not being able to
add auth to a request, we choose to fully respect the spec's hierachy of
security groups.

Change: Using `iken/httputil.ClientSecurityGroups`, requests for
operations with more than one security requirement will now be
authorized according to the first security requirement that has all of
it's security schemas' authorizers run without error. If no security
requirement can be met, the request will not be sent and an error will
be returned.
@codecov

codecov Bot commented Jul 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 21.05263% with 30 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@3c8c508). Learn more about missing BASE report.

Files with missing lines Patch % Lines
output/openapi.go 25.80% 23 Missing ⚠️
cfg/config.go 0.00% 7 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main      #71   +/-   ##
=======================================
  Coverage        ?   30.41%           
=======================================
  Files           ?       45           
  Lines           ?     2591           
  Branches        ?        0           
=======================================
  Hits            ?      788           
  Misses          ?     1794           
  Partials        ?        9           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@davidn
davidn marked this pull request as ready for review July 16, 2026 16:39
davidn added 2 commits July 17, 2026 15:39
We may often want multiple clients in the same module, so this PR allows
the generated client to have a prefix on the types.

This commit also adds an interface, allowing easier mocking.
Some openapi specs use x-go-type, referring to types defined in the
server. We don't want these when generating clients, so add a param to
ignore them. This is true by default in the openAPIClient process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant