Skip to content

field that starts with underscore generates a conflict #1767

Closed

Description

@gainsley

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.

Activity

  1. jamietanna commented on Sep 20, 2024

    @jamietanna
    Member

    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!

  2. added
    bug
    on Sep 20, 2024
  3. added this to the v2.5.0 milestone on Sep 20, 2024
  4. gainsley commented on Sep 20, 2024

    @gainsley
    Author

    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
    
  5. Tualua commented on Oct 27, 2024

    @Tualua

    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
    
  6. jamietanna commented on May 10, 2025

    @jamietanna
    Member

    This should be closed by #1822 - will try and follow up to double check

  7. jamietanna commented on May 11, 2025

    @jamietanna
    Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions