Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom struct fieldname #468

Open
octomad opened this issue Oct 20, 2021 · 3 comments
Open

Custom struct fieldname #468

octomad opened this issue Oct 20, 2021 · 3 comments

Comments

@octomad
Copy link
Contributor

octomad commented Oct 20, 2021

This is a feature request to support custom struct field names.

We have a situation where we are using allOf to reference a JSON schema that has a property with a similar name. After the name gets sanitized for Go, it ends up duplicating a field name that already exists.

Example:

parentschema.json

{
  "title": "node",
  "type": "object",
  "description": "Represents a node",
  "properties": {
    "@id": {
      "type": "string",
      "readOnly": true
    }
}

components in the OpenAPI Spec

components:
  schemas:
    resourcenode:
      title: resourcenode
      allOf:
        - $ref: ./parentschema.json
        - type: object
          properties:
            id:
              type: string
              format: uuid
              description: ID of the resource
              readOnly: true

Because there's a property in the parentschema.json called @id, and a property in the OpenAPI schema called id, the generated code will output

type Resourcenode struct {
	Id string `json:"@id"`
	Id string `json:"id"`
}

Which obviously won't compile because of the duplicate field in the struct. If we could add support for custom field names, that would help.

An alternative approach would be to add an X-Extension for x-go-fieldname-prefix to prefix the property so that it generates this instead

type Resourcenode struct {
	NodeId string `json:"@id"`
	Id string `json:"id"`
}
@octomad
Copy link
Contributor Author

octomad commented Oct 20, 2021

I don't mind implementing the change if you give me some pointers.

@emilekm
Copy link
Contributor

emilekm commented Feb 8, 2022

Hey @octomad, I've implemented the change you requested - please take a look and tell me if it satisfies you and if it works for your case.

@octomad
Copy link
Contributor Author

octomad commented Feb 8, 2022

SHIP IT! :shipit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants