oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Generated invalid code: "cannot use untyped string constant as *string value in assignment "

Open vitaly-zdanevich opened this issue 2 years ago • 6 comments

Generated invalid line:

v.Myvar = "none"

Definition:

Myvar *string `json:"my_var,omitempty"`

JSON Open API definition, related part:

      "A" : {
        "type" : "object",
        "properties" : {
          "my_var" : {
            "type" : "string",
            "example" : "none"
          },
          "filter_hash" : {
            "type" : "string",
          }
        }
      },
      "ANone" : {
        "allOf" : [ {
          "$ref" : "#/components/schemas/A"
        }, {
          "type" : "object",
          "properties" : {
            "filter_payload" : {
              "type" : "object",
              "description" : "When my_var is \"none\": empty object."
            }
          }
        } ]
      },

Version 2.0.0.

Gentoo Linux.

Installed by go install.

vitaly-zdanevich avatar Nov 24 '23 10:11 vitaly-zdanevich

Thanks for raising this - when you say:

Generated invalid line:

Would you mind sharing more before/after that line? Just trying to work out where that's being generated from

jamietanna avatar Dec 04 '23 15:12 jamietanna

Hey there, I seem to have the very same issue. I'm working on go-nautobot. I have this PR currently open: https://github.com/nautobot/go-nautobot/pull/27

There, we see a similar issue, I pasted the logs there. Should you need more information, feel free to ask :)

TobiPeterG avatar Jun 21 '24 21:06 TobiPeterG

I just ran into this using latest version.

In my case, the generator generated the following:


type TrackObject struct{
    // ommitted other fields for brevity
    // Type The object type: "track".
    Type *TrackObjectType `json:"type,omitempty"`
}

// TrackObjectType The object type: "track".
type TrackObjectType string

// FromTrackObject overwrites any union data inside the PlaylistTrackObject_Track as the provided TrackObject
func (t *PlaylistTrackObject_Track) FromTrackObject(v TrackObject) error {
	v.Type = "TrackObject" // <=========== ERROR HERE
	b, err := json.Marshal(v)
	t.union = b
	return err
}

And this results in the error

cannot use "TrackObject" (untyped string constant) as *TrackObjectType value in assignment

Hope this helps!

UPDATE:

In my case, I was generating source (just the models) for https://raw.githubusercontent.com/sonallux/spotify-web-api/refs/heads/main/fixed-spotify-open-api.yml

Thought this could be helpful context, maybe the problem is with the specification.

torbensky avatar Oct 18 '24 20:10 torbensky

@torbensky did you find a workaround for that issue? I'm having the same issue on another spec.

andiikaa avatar Jul 23 '25 07:07 andiikaa

Just hit the same issue with an OpenApi 3.0.0 contract that makes use of: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#discriminator-object with mapping + oneOf on the schema object.

So similar to: https://github.com/oapi-codegen/oapi-codegen/issues/1360#issuecomment-2423159871, but with mapping used aswell.

CraigAstillRVU avatar Jul 31 '25 14:07 CraigAstillRVU

@torbensky did you find a workaround for that issue? I'm having the same issue on another spec.

No, I ended up just rolling my own API client.

torbensky avatar Sep 17 '25 19:09 torbensky