You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
field that starts with underscore generates a conflict #1767
The OSM SOL005 API spec has a bunch of objects that define the following two ID fields:
Alarm:
description: |
Alarm Information
type: object
properties:
_id:
description: Identifier of the Alarm.
type: string
format: uuid
id:
description: Identifier of the Alarm.
type: string
format: uuid
The generated go code becomes:
// Alarm Alarm Information
type Alarm struct {
// Id Identifier of the Alarm.
Id openapi_types.UUID `json:"_id"`
// Id Identifier of the Alarm.
Id openapi_types.UUID `json:"id"`
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml SOL005.yaml
package osmapi
I believe there needs to be some logic that checks that renaming a field that starts with underscore doesn't conflict with the names of any other fields.
Btw this is a really great project, it's been very helpful for my work, thanks so much for creating it.
Thanks for your feedback, Jamie! Unfortunately we just started working on this, and we're only using a portion of the full API spec, so I have no idea if we actually need both IDs or not. For now I'm just using sed to rename one of the IDs in the generated golang code.
If it's helpful to anyone else, here is the sed command:
sed -i -E -e 's|(Id)(.+)(json:"_id)|Uid\2\3|g' osm-generated.go
Thanks for your feedback, Jamie! Unfortunately we just started working on this, and we're only using a portion of the full API spec, so I have no idea if we actually need both IDs or not. For now I'm just using sed to rename one of the IDs in the generated golang code.
If it's helpful to anyone else, here is the sed command:
sed -i -E -e 's|(Id)(.+)(json:"_id)|Uid\2\3|g' osm-generated.go
Thank you for the sed command! Do you know how it can be used in go generate? I tried to put it as is - it throws an error
sed: unsupported command '
internal/core/generate.go:4: running "sed": exit status 1
I managed that by putting sed script into file and calling sed with an -f option
//go:generate sed -i -E -f ../tools/fix_id.sed core-models.gen.go
Activity
jamietanna commentedon Sep 20, 2024
Out of interest, is there a useful distinction you get back from the API between the two fields?
If not, you could filter the spec (via)
I agree that this is something we should fix regardless!
gainsley commentedon Sep 20, 2024
Thanks for your feedback, Jamie! Unfortunately we just started working on this, and we're only using a portion of the full API spec, so I have no idea if we actually need both IDs or not. For now I'm just using sed to rename one of the IDs in the generated golang code.
If it's helpful to anyone else, here is the sed command:
Tualua commentedon Oct 27, 2024
Thank you for the sed command! Do you know how it can be used in go generate? I tried to put it as is - it throws an error
I managed that by putting sed script into file and calling sed with an -f option
jamietanna commentedon May 10, 2025
This should be closed by #1822 - will try and follow up to double check
jamietanna commentedon May 11, 2025
I can confirm this was closed by #1822 👍 I'll add a test case via this issue, too
chore(tests): add additional test case for underscore naming
chore(tests): add additional test case for underscore naming