OpenAPITools / OpenAPITools/openapi-generator

[BUG][GO] Imports for field types are not included when using inheritance/polymorphism

Open
#19,959 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?
  • 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?
    In the generated model_foo.go, we have
      import (
      	"encoding/json"
      )
    
    But should have
    import (
    	"encoding/json"
    	"time"
    )
    
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I found a case where the generated Go code doesn't compile, it is missing an import of the time package. I think it's due to an incorrect implementation of polymorphism.

openapi-generator version
$ docker run --rm openapitools/openapi-generator-cli:latest version
7.10.0-SNAPSHOT
OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: '1.0.0'
  title: 'FooBar Service'
paths: {}
components:
  schemas:
    Foo:
      type: object
      anyOf: [] # doesn't need to contain anything, the object's properties are dropped due to the presence of anyOf
      properties:
        bar:
          format: date-time
          type: string
Generation Details

I'm using

docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest generate \
        --input-spec /local/go-time-import.yaml \
        --generator-name go \
        --additional-properties "withGoMod=false" \
        --package-name gen \
        --output /local/gen
Steps to reproduce

If you run https://gist.github.com/perhallgren/d2af2441d7c265a06407313b064f68d6, you get:

[...]
gen/model_foo.go:22:7: undefined: time
gen/model_foo.go:43:24: undefined: time
gen/model_foo.go:45:11: undefined: time
gen/model_foo.go:53:28: undefined: time
gen/model_foo.go:70:24: undefined: time
Related issues/PRs
Suggest a fix

I'm working on https://github.com/OpenAPITools/openapi-generator/pull/19958, with a fix that seems to solve the problem I am having.

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 OpenAPI declaration and generation command in the issue, then inspect the generated gen/model_foo.go output and compare the proposed fix in PR #19958. Done means the generated Go model includes the required time import and compiles without the reported undefined: time errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.