OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.49k stars 6.51k forks source link

[BUG] [Golang] anyOf results in empty Go struct #2164

Open mroland91 opened 5 years ago

mroland91 commented 5 years ago
Description

When Go model code is generated for an "anyOf" type then the result will be an empty struct.

Yaml:

RatType: anyOf:

  • type: string enum:
  • NR
  • EUTRA
  • WLAN
  • VIRTUAL
  • type: string

Actual: type RatType struct { }

Expected:

package openapi
type RatType string

// List of RatType
const (
  NR RatType = "NR"
  EUTRA RatType = "EUTRA"
  WLAN RatType = "WLAN"
  VIRTUAL RatType = "VIRTUAL"
)
openapi-generator version

4.0.0-SNAPSHOT

OpenAPI declaration file content or url

https://github.com/jdegre/5GC_APIs/blob/7298845c7ede07038838d5c40debb083f929f98b/TS29571_CommonData.yaml#L548

https://github.com/jdegre/5GC_APIs/blob/78b04e48a7fb92b1a5dc6f95419c9ed2f6c3c5ed/TS29518_Namf_MT.yaml#L166

Command line used for generation

./run-in-docker.sh generate -i 5GC_APIs/TS29518_Namf_MT.yaml -g go -o /gen/out -Dmodels

Steps to reproduce
./run-in-docker.sh generate -i 5GC_APIs/TS29518_Namf_MT.yaml -g go -o /gen/out -Dmodels

cat out/model_rat_type.go
auto-labeler[bot] commented 5 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

goyalnikhil commented 5 years ago

Hello, I'm also blocked due to this issue in one of my development project. If this is not picked up yet, I can volunteer to handle it. A small hand holding or summary of the general area to look at would greatly help. Till then I will explore on my own.

Regards Nikhil

fantavlik commented 5 years ago

Hey @mroland91 and @goyalnikhil I think the issue is that the anyOf is not needed. If you remove it and use:

RatType:
  type: string
  enum:
  - NR
  - EUTRA
  - WLAN
  - VIRTUAL

You'll see the code generated as you expect:

package openapi
type RatType string

// List of RatType
const (
    RAT_TYPE_NR RatType = "NR"
    RAT_TYPE_EUTRA RatType = "EUTRA"
    RAT_TYPE_WLAN RatType = "WLAN"
    RAT_TYPE_VIRTUAL RatType = "VIRTUAL"
)
goyalnikhil commented 5 years ago

Hello @fantavlik Thanks for your response, You are right, removing the anyOf does generate the correct constants definition, but that's not going to help me unfortunately. My yaml is from an external source that I cant control, they are the API definition body which is following the OpenAPI v3.0 specs strictly. So there should be handling of anyOf, oneOf and allOf in the Go generator. I haven't tried other language generators to see if that works or not. Ideally all 3 should work for all supported.

mroland91 commented 5 years ago

@fantavlik, I'm in similar situation as @goyalnikhil. The definition that I've added as an example is handled by 3GPP and as far as I can see this is a valid OpenAPI spec. I think the generator should handle this case and generate the constants.

fantavlik commented 5 years ago

Yup agreed, since this is just equivalent to giving the model a type it seems like a case we should support. I put a PR up for the core fix since this seems to behave the same across languages: https://github.com/OpenAPITools/openapi-generator/pull/2897

goyalnikhil commented 5 years ago

Thanks @fantavlik I volunteer to be your beta tester, i will run some tests on your changes to see how it helps. Appreciate your help!

Regards Nikhil

JSarvesh commented 4 years ago

Hi @goyalnikhil Were you able to resolve this issue. I am also working on Nchf_Converged_charging and Common_data YAML files and still facing the same issue. Did you use any workaround to overcome this issue?

albertlieyingadrian commented 4 years ago

Any updates on this?

KabudoWiseMan commented 11 months ago

Any updates? I've got even worse output for the following data. Spec:

            "BotMetaIn":
            {
                "properties":
                {
                    "data":
                    {
                        "anyOf":
                        [
                            {
                                "type": "string",
                                "format": "json-string"
                            },
                            {
                                "type": "object"
                            }
                        ],
                        "title": "Data"
                    },
                    "expiredAt":
                    {
                        "type": "string",
                        "title": "Expiredat"
                    }
                },
                "type": "object",
                "required":
                [
                    "data",
                    "expiredAt"
                ],
                "title": "BotMetaIn"
            },

Output:

type Data struct {
    map[string]interface{} *map[string]interface{}
    string *string
}