oapi-codegen: Generated API code no longer compiles after upgrade form 1.12.4 to 1.13.2

To make a long story short, my code doesn’t compile after the upgrade (after many releases which were drama free). I have put up a demonstration of the problem here:

https://github.com/danielbprice/oapi-codegen-problem

I found that by changing application/problem+json to application/json in my openapi.yaml file, the problem goes away, so I suspect it’s related in some way to changes there. Thanks!

Possibly this could be related to what the user in https://github.com/deepmap/oapi-codegen/issues/1146 was talking about. Hopefully my reproducible test case will help.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 16 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@jamietanna, I can confirm that this passes the reduced test case, and also builds our in-house code correctly, as before, with all tests passing.

Many thanks to you and @deepmap-marcinr , I really get a lot out of having this tool available.

Thanks @oh0123 are you able to check if #1177 works for you too?

I’ve got some family over at the moment so will try and get the PR fixed up later today or tomorrow🤞🏽

sure, just test your PR in my case which only generate client,types code, and works fine. GOOD JOB.

@jamietanna I updated the LICENSE for the repository to Apache 2.0 and marked the file as Apache 2.0 licensed, except the included portion which is under MIT license.

@danielbprice looks like this may be due to the i.e. Misc400Error being newly generated in the v1.13.x release, as I don’t see it present in v1.12.x - I can reproduce with:

go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4  -o tmp/a.gen.go -package f openapi.yaml

That - as with your example - we see:

type GetV1TestResponse struct {
	Body         []byte
	HTTPResponse *http.Response
	JSON400      *ProblemDetails
	JSON404      *ProblemDetails
}
Slight tangent

What’s odd about this is that we’re missing the HTTP 200 response. Looks like this is due to:

  • using text/plain which doesn’t provide any bindings in the resulting response
  • not having a schema set correctly

With this change:

diff --git openapi.yaml openapi.yaml
index 51800bc..11b2f28 100644
--- openapi.yaml
+++ openapi.yaml
@@ -23,8 +23,9 @@ paths:
         '200':
           description: Successful response
           content:
-            text/plain:
-              type: string
+            application/json:
+              schema:
+                type: string
         '400':
           $ref: '#/components/responses/Misc400Error'
         '404':

We then get a JSON200.

This tangent ☝️ doesn’t seem to be the issue, just something I spotted and started investigating - still investigating

It looks like in v1.12.x we were determining that Misc400Error = ProblemDetails so just putting that inline, but now we’re expecting the Misc400Error to be there, but it’s not

Investigation seems to lead to https://github.com/deepmap/oapi-codegen/blob/f20166f0021f9784ede134799bc01e73e1c39e58/pkg/codegen/templates/client-with-responses.tmpl#L47-L53 being the root cause.

Further investigation shows that maybe it’s that we’re not correctly generating types from #/components/responses/

So yup, this is down to https://github.com/deepmap/oapi-codegen/blob/f20166f0021f9784ede134799bc01e73e1c39e58 pkg/codegen/codegen.go#L546 not triggering for application/problem+json

Thanks for catching and fixing! Now released as https://github.com/deepmap/oapi-codegen/releases/tag/v1.13.3