swagger-api / swagger-api/swagger-codegen-generators

Can we have a discussion about the Go code generator?

Open
#34 40 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
299
Forks
439
PR merge metrics
No merged PRs in 30d

Description

So I saw that 3.0.0 will use this repository for codegen generators. Version 2.3.x introduced some major breaking changes to the produced code. While most of these changes made sense, some of them are - in my opinion - bad choices.

I would like to use the chance that releasing 3.0.0 is, to start a discussion on providing a better code generator for Golang.

I'd like to start with some things that I think are really well done in the new 2.3.x release:

  1. It's now possible to provide a custom http.Client
  2. It's now possible provide a context.Context in API calls

I think there are also some issues.

  1. Redirects do apparently no longer work, because all status codes higher than 299 result in an error:
	if localVarHttpResponse.StatusCode >= 300 {
		bodyBytes, _ := ioutil.ReadAll(localVarHttpResponse.Body)
		return localVarHttpResponse, reportError("Status: %v, Body: %s", localVarHttpResponse.Status, bodyBytes)
	}
  1. Adding request bodies now requires passing a map[string]interface{} that needs, for example, a body key.

The second limitation is especially painful from an API point of view as type assertions are necessary to decode the payload, and the API is not very developer friendly in the context of Go. In real-world use it looks something like this:

CreateResource(context.Context, map[string]interface{}{"body": &SomeStruct{}})

I propse that instead, a type is used to solve this, for example:

type CreateResourceParameters struct {
  Body *SomeStruct
  Path ...
  Query ...
}

This would make the process of passing down request parameters much more usable and less error prone.

Let me know what you think.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the Go generator behavior described in the issue, especially status-code handling and map[string]interface{} request bodies. The first step is to gather maintainer direction on whether typed parameter structs and changed redirect handling are desired for 3.0.0. Done would require an agreed design, implementation scope, and validation criteria.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.