swagger-api / swagger-api/swagger-codegen
[Various] -DsupportingFiles generates incorrect code without -Dapis
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
Information about API operations is only calculated/computed when APIs are being generated, but some targets seem to have lists of operations in their supporting files. These targets cannot generate correctly generate their supporting files when -DsupportingFiles is specified by itself.
This affects the Go client and probably also JavaSpring, Go server, nodejs, and pythonFlaskConnexion.
Swagger-codegen version
3.0.25-SNAPSHOT
Swagger declaration file content or url
There is no specific OpenAPI code that triggers this problem, but here's a test one to make the commands to reproduce more complete.
openapi: 3.0.0
info:
title: "Example 01"
paths:
"/mibs/keys":
get:
summary: "List all MIB names"
responses:
'200':
description: Successful operation
content:
application/json:
schema:
type: string
Command line used for generation
java -cp $classpath
io.swagger.codegen.v3.cli.SwaggerCodegen
generate -l go -DsupportingFiles -i example01.yaml -o output/example02
Steps to reproduce
codegen() {
java -cp $classpath \
io.swagger.codegen.v3.cli.SwaggerCodegen \
generate "$@"
}
codegen -l go -Dapis -i example01.yaml -o output/example02 > /dev/null
codegen -l go -DsupportingFiles -i example01.yaml -o output/example02 > /dev/null
codegen -l go -Dmodels -i example01.yaml -o output/example02 > /dev/null
mkdir -p output/example02/test
cp example01.go output/example02/test/example02.go
cd output/example02
test -f go.mod || go mod init example01
test -f go.sum || go mod tidy
go build test/example02.go || echo Expected failure: "test/example02.go:14:28: client.DefaultApi undefined (type *swagger.APIClient has no field or method DefaultApi)"
The example01.go source file I used to test is here.
package main
import (
"context"
"example01" // Gets imported as "swagger"
)
func main() {
client := swagger.NewAPIClient(swagger.NewConfiguration())
client.DefaultApi.MibsKeysGet(context.TODO())
}
The client template for Go (main/resources/handlebars/go/client.mustache) has the following two blocks of Handlebars/Mustache code in it
{{#operations}}
{{classname}} *{{classname}}Service
{{/operations}}
and
{{#operations}}
c.{{classname}} = (*{{classname}}Service)(&c.common)
{{/operations}}
They expand to nothing unless APIs are being generated.
Related issues/PRs
Suggest a fix/enhancement
The obvious fix is to rewrite the target templates to not use the operations in supporting files. There is no good reason why the Go client target does this. This does change the target interface, but it is slightly cleaner than trying to stop processing the APIs halfway through so that you have all the operations without generating the APIs.
Or you could fiddle with the generateApis private method of the DefaultGenerator class. I will have a PR for this in a few minutes because it's probably the option you are going to pick.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the failure with example01.yaml and the supportingFiles-only commands, then inspect main/resources/handlebars/go/client.mustache and DefaultGenerator.generateApis. Compare the generated Go client with the APIs-plus-supportingFiles run; done means supporting files retain the required operations and the resulting example builds successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, java, openapi
- Domain
- backend-api-design, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100