Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mroland91 opened this issue Feb 15, 2019 · 10 comments
Open

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

mroland91 opened this issue Feb 15, 2019 · 10 comments

Comments

@mroland91
Copy link

mroland91 commented Feb 15, 2019

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
Copy link

auto-labeler bot commented Feb 15, 2019

👍 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
Copy link

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
Copy link
Contributor

fantavlik commented Mar 24, 2019

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
Copy link

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
Copy link
Author

@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
Copy link
Contributor

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: #2897

@goyalnikhil
Copy link

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
Copy link

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
Copy link

Any updates on this?

@KabudoWiseMan
Copy link

KabudoWiseMan commented Oct 23, 2023

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants