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

Feature request : describe a field with multiple types #1363

Open
maitredede opened this issue Oct 26, 2022 · 4 comments
Open

Feature request : describe a field with multiple types #1363

maitredede opened this issue Oct 26, 2022 · 4 comments

Comments

@maitredede
Copy link
Contributor

Hello,

Is your feature request related to a problem? Please describe.
I would like to describe a field with type any that can be filled with a list of types (like a C/C++ union). Example :

type SubOne struct {
}
type SubTwo struct {
}
type SubThree struct {
}
type Root struct {
	Field any //can be SubOne, SubTwo or SubThree
}

Describe the solution you'd like
I don't know if it is possible (in the spec), but I would like to tell swag to add these types in the generated data, and see that the Root.Field can be any of the given types.

Describe alternatives you've considered
I have not considered alternatives yet. At leat a way to include types not directly referenced.

Additional context
N/A.

@sdghchj
Copy link
Member

sdghchj commented Oct 31, 2022

Since swag follows swagger OpenAPI 2.0, while oneOf works in OpenAPI 3.0.

An alternative proposal is:

type SubOne struct {
}
type SubTwo struct {
}
type SubThree struct {
}
type Root struct {
        // type enums in [SubOne,SubTwo,SubThree]
	Field any  `oneof: "SubOne,SubTwo,SubThree"`
}

then, tell the given types in the field comment and output SubOne,SubTwo,SubThree into the definitions.

@harsh-98
Copy link

it doens't work.

@krasi-georgiev
Copy link

krasi-georgiev commented Apr 19, 2023

now that the v2 branch seems to have initial support for open API v3, any chance to add it?

Just updated to v2.0.0 and trying this with

type RequestReply struct {
	Params  any             `json:"params,omitempty" oneof:"SendBundleArgs,CancelBundleArgs"`
}

type SendBundleArgs struct {
	BlockNumber string `json:"blockNumber,omitempty"`
}

type CancelBundleArgs struct {
	ReplacementUUID string `json:"replacementUuid,omitempty"`
}

swag init --v3.1

produced a swagger file with no type for those

2023/04/19 13:51:02 Generating handler.RequestReply
found schema with no Type, returning any
handler.RequestReply:
      properties:
        error:
          $ref: '#/components/schemas/handler.Error'
        id:
          type: string
        jsonrpc:
          type: string
        method:
          type: string
        params: {}
        result:
          type: string
      type: object

@Nerzal
Copy link
Contributor

Nerzal commented Apr 19, 2023

The oneof feature is not yet implemented on the V2 branch.
My current goal for the V2 branch is to get feature parity between master & v2 branch.
Also I have a list of bugs in the V2 branch.

But if you want to add new features there, feel free to do so :)

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

5 participants