OpenAPITools / OpenAPITools/openapi-generator

[REQ][Go Client] Support circular schema definitions

Open
#5,737 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Go Enhancement: Compatibility Enhancement: Feature Feature: Composition / Inheritance Feature: OAS 3.0 spec support
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

Valid OpenAPI specs can have circular definitions, however, this is not currently supported in generated Go client SDKs.

Validity of OpenAPI circular definitions is discussed here:

Such definitions will also validate on the OAS3 validators by Mermade and APIDevTools.

Currently, when such a spec is used, the invalid recursive type compile error occurs.

# github.com/grokify/go-ringcentral/codegen/engagevoice
../../../../codegen/engagevoice/model_agent_account_access.go:12:6: invalid recursive type AgentAccountAccess
../../../../codegen/engagevoice/model_campaign.go:12:6: invalid recursive type Campaign
../../../../codegen/engagevoice/model_custom_dial_zone_groups.go:12:6: invalid recursive type CustomDialZoneGroups
../../../../codegen/engagevoice/model_quota_target.go:12:6: invalid recursive type QuotaTarget

Describe the solution you'd like

The problem with circular definitions occurs in Go because the compiler cannot determine the size of such a struct. The solution is to replace the nested struct with a pointer, who's size is known, as described here:

A pointer's size is known, but how big is something that contains itself? (And the inner struct contains itself as well, as does the inner inner struct, and so on.)

https://stackoverflow.com/a/8261789/1908967

This has been confirmed to compile by manually updating the generated model files.

Describe alternatives you've considered

None yet.

Additional context

  1. The use of pointers for nested structs is also being discussed in various JSON null value threads.
    1. https://github.com/OpenAPITools/openapi-generator/issues/522
    2. https://github.com/OpenAPITools/openapi-generator/issues/5278
  2. Google's auto-generated Go client SDKs appear to always use pointers for nested structs - https://github.com/googleapis/google-api-go-client

Given how pointers are part of a long-running nullable discussion, one thought is to use a CLI parameter that will make all nested structs pointers as to make it an optional feature.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported generated files model_agent_account_access.go, model_campaign.go, model_custom_dial_zone_groups.go, and model_quota_target.go, and reproduce the invalid recursive type errors for a circular OpenAPI definition. Compare them with the manually pointer-updated models described in the issue; done means the generated Go client compiles for these definitions without recursive-type errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, java
Domain
api, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.