opensearch-project / opensearch-api-specification

API specification for OpenSearch
Apache License 2.0
32 stars 51 forks source link

[FEATURE] Represent enums which have aliases #593

Open Xtansia opened 3 hours ago

Xtansia commented 3 hours ago

Is your feature request related to a problem?

There are enums that OpenSearch accepts where multiple string representations map to the same value, with one being the canonical representation (i.e. they're aliased).

Example: Current spec of GeoOrientation:

GeoOrientation:
      type: string
      enum:
        - left
        - right

Implementation of Orientation in OpenSearch:

public static Orientation fromString(String orientation) {
            orientation = orientation.toLowerCase(Locale.ROOT);
            switch (orientation) {
                case "right":
                case "counterclockwise":
                case "ccw":
                    return Orientation.RIGHT;
                case "left":
                case "clockwise":
                case "cw":
                    return Orientation.LEFT;
                default:
                    throw new IllegalArgumentException("Unknown orientation [" + orientation + "]");
            }
        }

Existing implementation of GeoOrientation in opensearch-java (on serialization first item is used, all accepted for deserialization):

public enum GeoOrientation implements JsonEnum {
    Right("right", "RIGHT", "counterclockwise", "ccw"),

    Left("left", "LEFT", "clockwise", "cw");
}

Note that we also handle all-caps variants as if OpenSearch itself sets and returns its version it will be uppercase.

This means the final spec should be roughly equivalent to:

GeoOrientation:
      type: string
      enum:
        - left
        - LEFT
        - clockwise
        - cw
        - right
        - RIGHT
        - counterclockwise
        - ccw

But should also communicate that left and right are the canonical versions and others map onto them.

Xtansia commented 3 hours ago

What are your thoughts @dblock @nhtruong?

Xtansia commented 3 hours ago

We could potentially re-use the oneOf concept like was used for deprecated enums and combine it with the title property:

GeoOrientation:
  oneOf:
    - title: left
      type: string
      enum:
        - left
        - LEFT
        - clockwise
        - cw
    - title: right
      type: string
      enum:
        - right
        - RIGHT
        - counterclockwise
        - ccw