3

Let's say in my OpenAPI v3 description, I have the following /componenets/schemas entry (using YAML) format:

WidgetTypes:
  type: string
  enum:
    - WIDGET_A
    - WIDGET_B
    - WIDGET_C

The WidgetTypes schema is thus a named (i.e. value $ref) enum 'class'. In various other places in the API spec, we can now refer to these enum values, e.g. perhaps there's an API path for which one of the path elements must come from the WidgetTypes set.

Now, I have also some additional schemas (i.e. object data models), and there might be a case where a specific WidgetType value is a constant for that object type. An example:

MySpecificWidgetA:
  type: object
  properties:
    someField1:
      type: string
    someField2:
      type: number
    widgetType:
      type: string
      enum:
        - WIDGET_A

This feels like the naïve way to accomplish this, since now MySpecificWidgetA's widgetType field is a string that comes from the set of possible WidgetType's, but there's no actual reference to WidgetType enforcing this. In spirit, what I'd like to assert is that MySpecificWidgetA.widgetType is a specific value from the WidgetType enum schema (in this case WIDGET_A). Using just $ref: '#/components/schemas/WidgetType' passes validation, but doesn't accomplish what I want: it states widgetType is simply a value coming from that set ... I want it to be a restricted value from that set (i.e. a constant).

I've tried experimenting with $ref a few other ways for the value of widgetType, including (without success):

$ref: '#/components/schemas/WidgetType/WIDGET_A'
$ref: '#/components/schemas/WidgetType/enum/WIDGET_A'
$ref: '#/components/schemas/WidgetType/enum/0' 

(the last one being not very useful, but just testing out the exact JSON Pointer format that $ref uses.)

None of these attempts above pass the OpenAPI v3 validation ... does anyone know if it's possible to reference (via $ref or some other mechanism) a specific value from a defined enum schema element?

mmuurr
  • 1,310
  • 1
  • 11
  • 21
  • I don't think this was a duplicate. It's very similar, but this question is worded to be more about the capability of the `$ref` keyword than enums directly. – gregsdennis Nov 20 '20 at 18:36

1 Answers1

5

This is not allowed by JSON Schema. $ref is only allowed to resolved to other schema objects, not to individual data points.

gregsdennis
  • 7,218
  • 3
  • 38
  • 71