Generated invalid code: "cannot use untyped string constant as *string value in assignment "
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.
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
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 :)
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 did you find a workaround for that issue? I'm having the same issue on another spec.
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.
@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.