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

field that starts with underscore generates a conflict

Open gainsley opened this issue 1 year ago • 2 comments

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

Which causes a compile error.

For reference, using config.yaml of

package: osmapi
generate:
  models: true
  client: true
output: osm-generated.go

and gen.go:

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

gainsley avatar Sep 20 '24 00:09 gainsley

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!

jamietanna avatar Sep 20 '24 15:09 jamietanna

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

gainsley avatar Sep 20 '24 18:09 gainsley

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

Tualua avatar Oct 27 '24 06:10 Tualua

This should be closed by https://github.com/oapi-codegen/oapi-codegen/pull/1822 - will try and follow up to double check

jamietanna avatar May 10 '25 21:05 jamietanna

I can confirm this was closed by #1822 👍 I'll add a test case via this issue, too

jamietanna avatar May 11 '25 10:05 jamietanna