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

Reserved golang keywords in spec cause errors #30

Closed
stevenvegt opened this issue Jun 24, 2019 · 3 comments
Closed

Reserved golang keywords in spec cause errors #30

stevenvegt opened this issue Jun 24, 2019 · 3 comments

Comments

@stevenvegt
Copy link

I have a type parameter in my spec as follows:

/animal/{type}:
    get:
      summary: Get an animal by type
      parameters:
        - name: type
          in: path
          required: true
          schema:
            type: string

Which resuls in the following server interface:

type ServerInterface interface {
  GetAnimalByType(ctx echo.Context, type string, params GetAnimalByTypeParams) error
}

But as you can see this results in a parameter named type which is a reseverd golang keyword.

Perhaps pre/post fixing all param names can prevent these errors.

@deepmap-marcinr
Copy link
Contributor

deepmap-marcinr commented Jun 24, 2019

There are many cases where this will happen, so I'm thinking of side-stepping the problem by allowing people to specify the Go variable name explicitly via extended properties. You could do something like:

/animal/{type}:
    get:
      summary: Get an animal by type
      parameters:
        - name: type
          x-golang-name: animalType
          in: path
          required: true
          schema:
            type: string

which would yield

type ServerInterface interface {
  GetAnimalByType(ctx echo.Context, animalType string, params GetAnimalByTypeParams) error
}

I'm in the middle of a big project right now, but I'll revisit this sometime soon.

@stevenvegt
Copy link
Author

The problem here is that you do not always have control over the spec as it can be created by a third party. Adding language specific tags sounds like a hack.

Postfixing every param with Param for example would also result in an unique non-confilicting string.

If we can think up an elegant solution I'm happy to provide a PR if that helps you.

@deepmap-marcinr
Copy link
Contributor

You're right.

I didn't want to break existing clients, so in the case of parameters which are go keywords, I prefix those with p, so type becomes pType. Non-conflicting names are not prefixed.

adrianpk pushed a commit to foorester/oapi-codegen that referenced this issue Jan 16, 2024
When a Swagger spec defines a variable name which conflicts
with a Go keyword, prefix the Go keyword to avoid a syntax
error.
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