Skip to content

Properly using oneOf/anyOf/etc. #136

Answered by vearutop
NuSkooler asked this question in Q&A
Discussion options

You must be logged in to vote

Hello, *Of schema documentation is separated from actual parent structure, so you need to implement custom (un)marshaler too.
Please check example (I also used generic API here, as it is now the recommended way to define an interactor).

package usecase

import (
	"context"
	"encoding/json"

	"github.com/swaggest/jsonschema-go"
	"github.com/swaggest/usecase"
)

type OneOfViewData struct {
	// Enumerate structures and disable default marshaling.
	S1 *PossibleStruct1 `json:"-"` 
	S2 *PossibleStruct2 `json:"-"`
}

// MarshalJSON implements oneOf marshaling.
// UnmarshalJSON is skipped as this is only a response structure.
func (o OneOfViewData) MarshalJSON() ([]byte, error) {
	if o.S1 != nil

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@NuSkooler
Comment options

Answer selected by NuSkooler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants