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

Annotate structures with extensions #2409

Open
emaincourt opened this issue Sep 29, 2020 · 1 comment
Open

Annotate structures with extensions #2409

emaincourt opened this issue Sep 29, 2020 · 1 comment
Labels
enhancement generate spec Related to spec generation from code

Comments

@emaincourt
Copy link

Problem statement

Hi,

First of all, thanks for your great work in go-swagger. I've been using it over the past months and I really had a great time. However I'm currently trying to annotate structures with Extension-related stuff and can not get it to work. I actually found this issue which might be related but not sure if it is still relevant #2106.

My goal is to get a given structure to be imported from its original package. In yaml spec, it gives us:

MyType:
  x-go-type:
    import:
      package: github.com/org/repo/pkg/name
    type: MyType

However, if I try the following:

// swagger:model
// Extensions:
// ---
// x-go-type:
//   import:
//     package: github.com/org/repo/pkg/name
//   type: MyType
// ---
type MyType struct {
	ID uuid.UUID `json:"id"`
}

And then run swagger generate spec --include=name -m -o swagger.yaml, what I get is the following:

  MyType:
    properties:
      id:
        $ref: '#/definitions/UUID'
    type: object
    x-go-package: github.com/org/repo/pkg/name

Is it something that is kind of expected ? I am missing something here ?

Thanks in advance

Environment

swagger version: v0.25.0
go version: go1.15.2 darwin/amd64
OS: macOS

@fredbi fredbi added generate spec Related to spec generation from code enhancement labels Nov 14, 2020
@szab100
Copy link

szab100 commented May 2, 2023

Hi, @fredbi sorry, but can I ask what is going on with these issues? I just attempted to use go-swagger in our project yesterday and ran into each of these problems when generating spec from annotated code.

What I was trying to solve is that I generate the swagger.json from our API project using the annotations on the model structs. And use this swagger.json to generate api client code in our other project, but instead of generating the models on the client side, I want the GO packages to be imported instead! Without that feature, this library seems completely useless.

I tried manually adding the x-go-type extension to a model object in the definitions section, and indeed, the library avoided generation of that model object & just imported the specified type instead. This is good, but obviously we cannot add these manually to each types, we need an automated solution when generating our swagger spec. Therefore, like many others, I also tried to add the x-go-type extension to my model structs similarly to the above, but it seems like the "Extensions:" comment is completely ignored in most cases (if I change the type to anything else than what's in the concrete code example). I also failed to understand what is the point of automatically adding the x-go-package extension IF the api-client generator completely ignores it and still generates all the models instead of simply using this extra information and importing the package (we would not need x-go-type if this was just working with x-go-package out of the box).

Can you please share your vision & thoughts on these issues? Maybe we are all doing something wrong, but it looks like this library is in active development, still these core reported issues are being ignored for years. Is it worth waiting for any kind of solutions to this very basic problem, eg. generating swagger.json from/to code with importing models rather than duplicating on client side? I also read that Golang 1.19 has slightly changed go docs/comment formatting style that might have broken certain things with this project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement generate spec Related to spec generation from code
Projects
None yet
Development

No branches or pull requests

3 participants