Openapi-generator: [BUG] [Golang] anyOf results in empty Go struct

Created on 15 Feb 2019  路  9Comments  路  Source: OpenAPITools/openapi-generator

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
Go Bug

All 9 comments

馃憤 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.

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

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"
)

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.

@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.

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

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

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?

Any updates on this?

Was this page helpful?
0 / 5 - 0 ratings