Skip to content

[GO] Self-referencing definitions result in generating 'recursive' and thus invalid structs #3565

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

Closed
ivucica opened this issue Aug 9, 2016 · 6 comments

Comments

@ivucica
Copy link
Contributor

ivucica commented Aug 9, 2016

Description

Because structs and not pointers to structs are used in the generated code, a definition cannot refer to itself.

Swagger-codegen version

7a245e3

Swagger declaration file content or url

Full file available at https://bitbucket.org/api/swagger.json (it may change, but that's out of my control)

Also https://bitbucket.org/ivucica/calendarify/src/f1297e3411fcb52e830bd02dd0108e85eed39488/vendor/bitbucket.org/api/swagger.json

Command line used for generation

See https://bitbucket.org/ivucica/calendarify/src/f1297e3411fcb52e830bd02dd0108e85eed39488/vendor/bitbucket.org/api/BUILD

genrule(
    name = "rest_client_go_gen",
    srcs = [":swagger.json"],
    outs = SWAGGER_GENERATED,
    tools = [
        "//vendor:swagger-codegen-cli",
        "//vendor:swagger-codegen-cli_deploy.jar",
    ],
    cmd = "\n".join([
        "mkdir -p \"$$(dirname $(location :configuration.go))\"",
        "$(location //vendor:swagger-codegen-cli) --singlejar generate " +
        " -i $(location :swagger.json)" +
        " -l go" +
        " -o \"$$(dirname $(location :configuration.go))\"",
    ]),
)

Versions of dependencies: https://bitbucket.org/ivucica/calendarify/src/f1297e3411fcb52e830bd02dd0108e85eed39488/WORKSPACE

Versions of swagger projects: https://bitbucket.org/ivucica/calendarify/src/f1297e3411fcb52e830bd02dd0108e85eed39488/vendor/github.com/swagger-api/

  • swagger-codegen [7a245e3]
  • swagger-core [c7c9a4c641dc]
  • swagger-parser [0e2fac8d6e46]
  • swagger-ui [44149467753f]
Steps to reproduce

Bitbucket's API contains the following definition (transformed from JSON into YAML by the Swagger editor):

  repository:
   allOf:
      - $ref: '#/definitions/object'
      - additionalProperties: true
        type: object
        description: A Bitbucket repository.
        properties:
          ...
          created_on:
            type: string
            format: date-time
          parent:
            $ref: '#/definitions/repository'
          full_name:
            type: string
            description: >-
              The concatenation of the repository owner's username and the
              slugified name, e.g. "evzijst/interruptingcow". This is the same
              string used in Bitbucket URLs.
          ...

Generated code looks like:

type Repository struct {
...
          Parent Repository `json:"parent,omitempty"`
...
}

This results in a build error in Go such as:

badc0de.net/pkg/calendarify/bazel-out/local-fastbuild/genfiles/vendor/bitbucket.org/api/repository.go:69: invalid recursive type Reposit
ory
Related issues

I am unaware of any.

Suggest a Fix

Consider updating the generated code to have properties of definitions generated as pointer types of struct fields.

type Repository struct {
...
          Parent *Repository `json:"parent,omitempty"`
...
}

To maintain compatibility with preexisting code that assumes fields are not pointers, this could be done only in case a definition refers to itself.

@wing328
Copy link
Contributor

wing328 commented Aug 10, 2016

@ivucica thanks for the suggestion. I believe a similar discussion is #2330

@ivucica
Copy link
Contributor Author

ivucica commented Aug 10, 2016

It is indeed related. This is indeed an additional reason to use pointers.

ivucica added a commit to ivucica/swagger-codegen that referenced this issue Aug 10, 2016
We could try to avoid this for some base types, but Go's JSON encoder
should treat this well enough.

Addresses swagger-api#3565 and swagger-api#2330.
@wing328
Copy link
Contributor

wing328 commented Aug 11, 2016

cc @neilotoole @guohuang

ivucica added a commit to ivucica/swagger-codegen that referenced this issue Aug 23, 2016
We could try to avoid this for some base types, but Go's JSON encoder
should treat this well enough.

Addresses swagger-api#3565 and swagger-api#2330.
@wing328 wing328 modified the milestones: Future, v2.2.2 Feb 16, 2017
@wing328
Copy link
Contributor

wing328 commented May 1, 2017

@ivucica when you've time I wonder if you can check out 2.3.0 branch to generate Go API client, which has been rewritten by @antihax with a lot of enhancements, and let us know if you've any feedback. Thanks!

@antihax
Copy link
Contributor

antihax commented Dec 27, 2017

Current master produces code that compiles for this specification now.

@wing328 wing328 modified the milestones: Future, v2.3.0 Dec 27, 2017
@wing328
Copy link
Contributor

wing328 commented Dec 27, 2017

@ivucica please pull the latest master to give it a try.

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

No branches or pull requests

3 participants