OpenAPITools / OpenAPITools/openapi-generator

[BUG][Go] anon req bodies unified under one schema

Open
#14,786 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

If two anonymous body request definitions share the same structure, they are named and represented by one structure in generated code.

The name is based on first occurance, what creates hidious API.
The worst effect is that it if you add property as part of API minor release (semver), the generated SDK isn't backward compatible

openapi-generator version

All what I have tried, including master.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: 'Foo-bar'
  version: 1.0.0
paths:
  /foo:
    post:
      tags:
      - foo
      operationId: operationFoo
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                value:
                  type: string
      responses:
        '200':
          description: 'foo'
  /bar:
    post:
      tags:
      - bar
      operationId: operationBar
      requestBody:
        content:
          application/json:
            schema:
              properties:
                value:
                  type: string
              type: object
      responses:
        '200':
          description: 'bar'
  /baz:
    post:
      tags:
      - baz
      operationId: operationBaz
      requestBody:
        content:
          application/json:
            schema:
              properties:
                value:
                  type: string
              type: object
      responses:
        '200':
          description: 'baz'
Steps to reproduce
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
UID:=$(shell id -u)
GID:=$(shell id -g)

#version:=v6.3.0
version:=latest

all: clean gen

gen:
	docker run --rm \
		--user ${UID}:${GID} \
		-v "${ROOT_DIR}:/local" \
		openapitools/openapi-generator-cli:${version} generate \
        	-i /local/swagger.yaml \
        	-g go \
        	-o /local/out
	cat out/api_bar.go | grep --color -E -n5 "operationFooRequest"
	cat out/api_baz.go | grep --color -E -n5 "operationFooRequest"

clean:
	-rm -rf ${ROOT_DIR}/out

Output
cat out/api_bar.go | grep --color -E -n5 "operationFooRequest"
23-type BarApiService service
24-
25-type ApiOperationBarRequest struct {
26-     ctx context.Context
27-     ApiService *BarApiService
28:     operationFooRequest *OperationFooRequest
29-}
30-
31:func (r ApiOperationBarRequest) OperationFooRequest(operationFooRequest OperationFooRequest) ApiOperationBarRequest {
32:     r.operationFooRequest = &operationFooRequest
33-     return r
34-}
35-
36-func (r ApiOperationBarRequest) Execute() (*http.Response, error) {
37-     return r.ApiService.OperationBarExecute(r)
--
85-     localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
86-     if localVarHTTPHeaderAccept != "" {
87-             localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
88-     }
89-     // body params
90:     localVarPostBody = r.operationFooRequest
91-     req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
92-     if err != nil {
93-             return nil, err
94-     }
95-
cat out/api_baz.go | grep --color -E -n5 "operationFooRequest"
23-type BazApiService service
24-
25-type ApiOperationBazRequest struct {
26-     ctx context.Context
27-     ApiService *BazApiService
28:     operationFooRequest *OperationFooRequest
29-}
30-
31:func (r ApiOperationBazRequest) OperationFooRequest(operationFooRequest OperationFooRequest) ApiOperationBazRequest {
32:     r.operationFooRequest = &operationFooRequest
33-     return r
34-}
35-
36-func (r ApiOperationBazRequest) Execute() (*http.Response, error) {
37-     return r.ApiService.OperationBazExecute(r)
--
85-     localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
86-     if localVarHTTPHeaderAccept != "" {
87-             localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
88-     }
89-     // body params
90:     localVarPostBody = r.operationFooRequest
91-     req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
92-     if err != nil {
93-             return nil, err
94-     }
95-

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

Run the supplied Makefile reproduction using swagger.yaml and inspect out/api_bar.go and out/api_baz.go, focusing on the generated operation request types. Done means equivalent anonymous request bodies receive distinct, operation-specific generated types so adding a property to one does not alter the other.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.