OpenAPITools / OpenAPITools/openapi-generator

[BUG][Go][Server] Remove Duplicate Imports

Open
#11,622 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
openapi: 3.0.0
info:
  description: >-
    Test to replicate duplicate os imports.
  version: 1.0.0
  title: Duplicate Imports
  license:
    name: Apache-2.0
    url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
paths:
  /file1:
    post:
      tags:
        - file1
      summary: uploads an image
      description: ''
      operationId: postFile1
      responses:
        '200':
          description: successful operation
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                additionalMetadata:
                  description: Additional data to pass to server
                  type: string
                file:
                  description: file to upload
                  type: string
                  format: binary
  /file2:
    post:
      tags:
        - file2
      summary: uploads an image
      description: ''
      operationId: postFile2
      responses:
        '200':
          description: successful operation
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                additionalMetadata:
                  description: Additional data to pass to server
                  type: string
                file:
                  description: file to upload
                  type: string
                  format: binary
  • Have you validated the input using an OpenAPI validator (example)?
  • 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?

Expected Output:

package openapi

import (
	"context"
	"net/http"
	"os"
)

Actual Output:

package openapi

import (
	"context"
	"net/http"
	"os"
	"os"
)
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

While trying to create a PR for a separate issue, I discovered that go-server (and potentially some of the other go generators have an issue with postProcessOperationsWithModels. This method can create duplicate import mappings in go-server's api.go if two separate tags try and create the same mapping.

openapi-generator version

Tried on 6.0.0-SNAPSHOT. It doesn't appear to be a regression.

OpenAPI declaration file content or url

Yaml posted above.

Generation Details
Steps to reproduce
./run-in-docker.sh java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate --input-spec test.yaml --generator-name go-server --output generated
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/pull/3847

Suggest a fix

Trying to figure out a fix for this right now as I'm about to create a PR that makes the issue more obvious. I don't think postProcessOperationsWithModels is necessarily the problem but I'm not sure where else the imports could be added.

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 provided run-in-docker.sh generation command with the YAML reproduction, then trace go-server's postProcessOperationsWithModels and the import mappings it produces. Done means the generated api.go contains each import only once, matching the expected output shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.