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

Invalid client code generation for referenced enum properties #2956

Open
cstaud opened this issue Jul 21, 2023 · 2 comments
Open

Invalid client code generation for referenced enum properties #2956

cstaud opened this issue Jul 21, 2023 · 2 comments
Labels
allOf enum model Related to swagger generate model command

Comments

@cstaud
Copy link

cstaud commented Jul 21, 2023

Problem statement

When creating the client code for a parameter referencing an object which has a string property referencing an enum string, the go property is created as a struct instead of a (typed) string.

Swagger specification

{
  "swagger": "2.0",
  "info": {
    "title": "test",
    "version": "1"
  },
  "paths": {
    "/test": {
      "put": {
        "consumes": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "test",
            "in": "body",
            "schema": {
              "$ref": "#/definitions/myParams"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  },
  "definitions": {
    "myParams": {
      "type": "object",
      "properties": {
        "myString": {
          "allOf": [
            {
              "$ref": "#/definitions/myStringEnum"
            }
          ]
        }
      }
    },
    "myStringEnum": {
      "type": "string",
      "enum": [
        "enumValue1",
        "enumValue2"
      ],
      "x-enum-varnames": [
        "EnumName1",
        "EnumName2"
      ]
    }
  }
}

Steps to reproduce

  • generate the client code for the spec
  • the MyParams struct has a property MyString of type struct:
type MyParams struct {

	// my string
	MyString struct {
		MyParamsMyStringAllOf0
	} `json:"myString,omitempty"`
}
  • when marshaling this it results in something like: {"MyString":{"MyParamsMyStringAllOf0":"enumValue1"}} , but the spec describes something like {"MyString":"enumValue1"}

Environment

swagger version: v0.30.5
go version: 1.20.6
OS: linux

@fredbi fredbi added model Related to swagger generate model command enum labels Dec 21, 2023
@fredbi
Copy link
Contributor

fredbi commented Dec 30, 2023

Testing this on latest master and against v0.30.5, here is the generated model:

type MyParams struct {

	// my string
	MyString struct {
		MyStringEnum
	} `json:"myString,omitempty"`
}

so this looks good to me.

Notice that without the allOf nesting, the generated code is slightly better looking:

type MyParams struct {

	// my string
	MyString MyStringEnum `json:"myString,omitempty"`
}

That's because models interpret "allOf" as mechanism to compose types.
There used to be plans to "lift" simple "allOf" structures like this one in their parent, but that path was never quite completed.

@fredbi fredbi added the allOf label Dec 30, 2023
@cstaud
Copy link
Author

cstaud commented Feb 14, 2024

Hi @fredbi, thank you for your comment and sorry for the late reply.

I can confirm the generated code you gave. But that is not my point. The thing were I struggle is:
Marshalling this:

type MyParams struct {
	// my string
	MyString struct {
		MyStringEnum
	} `json:"myString,omitempty"`
}

leads to:
{"MyString":{"MyStringEnum":"enumValue1"}}

But correct would be:
{"MyString":"enumValue1"}

Note: The swagger UI handles this also correctly and results in the correct payload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
allOf enum model Related to swagger generate model command
Projects
None yet
Development

No branches or pull requests

2 participants