question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG][GO] Generating golang models from openapi 3.3 containing oneOf results in undefined struct

See original GitHub issue

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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Using the generator to generate models for golang it generates models that are not defined. I am unsure if this is configuration or limitation.

openapi-generator version

Openapi-generator-cli docker-hub: Digest:sha256:829f0b601005c28950ed55c65d853f2caead265730782b2c7c48812bf8135222

OpenAPI declaration file content or url
{
  "openapi": "3.0.3",
  "info": {
    "title": "Exampleapi",
    "version": "2.0.0"

  },
  "paths": {
    "/temp": {
      "get": {
        "responses": {
          "200": {
            "description":"Tmp",
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Foo": {
        "type": "object",
        "properties": {
          "Bar": {
            "oneOf": [
              {"$ref": "#/components/schemas/FooBar"},
              {"$ref": "#/components/schemas/BarFoo"}
            ]
          }
        }
      },
      "FooBar": {
        "type": "object",
        "title": "FooBar",
        "properties": {
          "example": {
            "type": "string"
          }
        }
      },
      "BarFoo": {
        "type": "object",
        "title": "BarFoo",
        "properties": {
          "example": {
            "type": "string"
          }
        }
      }
    }
  }
}
Generation Details

Generation command

openapi-generator-cli generate \
        -i "/swagger.json" \
        -g go \
        --global-property models,supportingFiles=utils.go,modelDocs=false \
        --additional-properties  \
        --minimal-update \
        -p enumClassPrefix=true \
        --enable-post-process-file \
        --additional-properties packageName="Some Name" \
        -o "/Some_Location"
Steps to reproduce

Run above code on above swagger to replicate. The result given the expectations will result in:

type Foo struct {
    Bar *OneOfFooBarBarFoo `json:"Bar,omitempty"` <-- Undefined
}

Where OneOfFooBarBarFoo is not defined.

Related issues/PRs

No

Suggest a fix

Given go does not support generics a resonable response would be:

type Foo struct {
    Bar *interface{} `json:"Bar,omitempty"`
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:11

github_iconTop GitHub Comments

5reactions
bmason42commented, Apr 15, 2021

Same issue, any progress seen on this?

3reactions
ligertsicatcommented, Mar 9, 2022

Leaving this in case anyone else runs into this bug (top result from google), since it’s unclear.

Using oneOf directly causes problems when generating the sdk

requestBody: content: application/json: schema: oneOf: - $ref: ‘#/components/schemas/Object1’ - $ref: ‘#/components/schemas/Object2’

However, this can be fixed by placing the above into a separate object

requestBody: content: application/json: schema: $ref: ‘#/components/schemas/Object3’

Object3: oneOf: - $ref: ‘#/components/schemas/Object1’ - $ref: ‘#/components/schemas/Object2’

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generating from openapi to golang using ... - Stack Overflow
Generating golang code using openapi-generator-cli on an openapi 3.3 spec containing oneOf keyword results broken and uncompilable code.
Read more >
Generating Go structs from OpenAPI schemas - Jamie Tanna
How to generate Go structs really quickly and easily, with no manual work, using the oapi-codegen project.
Read more >
Go Code Generation from OpenAPI spec - Habr
OpenAPI specification is a modern industrial standard for REST API. This standard has fantastic tooling support and allows you to conveniently ...
Read more >
REST API implementations in Golang with OpenAPI ...
Tools to generate code and documentation for Go API servers and use a gRPS-gateway to build a server using gRPC, REST API, WebSocket, ......
Read more >
openapi3 - Go Packages
Validate returns an error if Components does not comply with the OpenAPI spec. type Contact ¶. type Contact struct { ExtensionProps `json:"-" ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found