github.com/go-swagger/go-swagger@v0.31.0/docs/reference/annotations/discriminated.md (about)

     1  ---
     2  title: discriminated
     3  date: 2023-01-01T01:01:01-08:00
     4  draft: true
     5  ---
     6  # swagger:discriminated
     7  
     8  Marks an embedded type as  a member for allOf and sets the x-class value. On interface definitions there is another annotation on methods allowed _swagger:name_
     9  
    10  <!--more-->
    11  
    12  The swagger:allOf annotation can be followed by a string. This string will be the value for the `x-class` vendor extension. This value is used as constant for the discriminator field.
    13  
    14  An interface that is embedded expects to have 1 method that is commented with `Discriminator: true`. That field will be used as discriminator field when marshalling/unmarshalling objects.
    15  
    16  Because this is for use with interfaces we can't make use of the json struct tag to allow for spec name overriding. So instead you can annotate method names on an interface with swagger:name and a value this will then provide the json field name.
    17  
    18  ##### Syntax
    19  
    20  ```go
    21  swagger:allOf org.example.something.TypeName
    22  ```
    23  
    24  ##### Example
    25  
    26  ```go
    27  // TeslaCar is a tesla car
    28  //
    29  // swagger:model
    30  type TeslaCar interface {
    31  	// The model of tesla car
    32  	//
    33  	// discriminator: true
    34  	// swagger:name model
    35  	Model() string
    36  
    37  	// AutoPilot returns true when it supports autopilot
    38  	// swagger:name autoPilot
    39  	AutoPilot() bool
    40  }
    41  
    42  // The ModelS version of the tesla car
    43  //
    44  // swagger:model modelS
    45  type ModelS struct {
    46  	// swagger:allOf com.tesla.models.ModelS
    47  	TeslaCar
    48  	// The edition of this Model S
    49  	Edition string `json:"edition"`
    50  }
    51  
    52  // The ModelX version of the tesla car
    53  //
    54  // swagger:model modelX
    55  type ModelX struct {
    56  	// swagger:allOf com.tesla.models.ModelX
    57  	TeslaCar
    58  	// The number of doors on this Model X
    59  	Doors int32 `json:"doors"`
    60  }
    61  ```
    62  
    63  ##### Result
    64  
    65  ```yaml
    66  ```