OpenAPITools / OpenAPITools/openapi-generator

[BUG][GO] Name collisions when schemas MySchema and NewMySchema exist

Open
#21,857 2 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When generating a go sdk for an API spec, there will be name collisions between functions generated and type names.
Namely, given an API spec with two types:

components:
  schemas:
    MySchema:
      # [...]
    NewMySchema:
      # [...]
# [...]

the resulting SDK code contains both a struct NewMySchema and a function NewMySchema (the latter of which creates a new MySchema object):

type NewMySchema struct {
// [...]
}
func NewMySchema(name string) *MySchema {
// [...]
}
openapi-generator version

v7.9.0

OpenAPI declaration file content or url
components:
  schemas:
    MySchema:
      properties:
        name:
          type: string
      title: "MySchema"
      type: object
      required:
        - name
    NewMySchema:
      properties:
        name:
          type: string
      title: "NewMySchema"
      type: object
      required:
        - name

info:
  title: Name Collision Test
  version: 0.1.0
openapi: 3.0.2
paths: 
  /test/:
    get:
      operationId: test
      responses:
        '200':
          description: Ensure MySchema is being used
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MySchema'
        '201':
          description: Ensure NewMySchema is being used
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/NewMySchema'
      tags:
        - test
Generation Details

See below (Steps to reproduce).

Steps to reproduce
  1. Save the above minimal API spec as ./testapispec.yaml
  2. Run the generator like:
    java -jar ./openapi-generator-cli.jar generate -i testspec.yaml -g go -o ./testspec/go --additional-properties=projectName=name-collisions-test,packageName=name_collisions_test
    
Related issues/PRs

I have found none.

Suggest a fix

I suppose, one way to fix this would be to create a new package (and therefore namespace) for each schema struct (and its functions). But maybe there's an easier way ...

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

Start with the minimal OpenAPI YAML and the documented generator command, then inspect the generated Go SDK for how schema types and constructor functions are named. Done means the MySchema and NewMySchema schemas generate without a type/function name collision and both referenced response types remain usable.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.