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

Formats for arrays #3572

Open
HenryGessau opened this issue Feb 15, 2024 · 6 comments
Open

Formats for arrays #3572

HenryGessau opened this issue Feb 15, 2024 · 6 comments

Comments

@HenryGessau
Copy link

Across various use cases for arrays, we find that in some situations the order is sensitive (for example, when the array functions as a vector, tuple, series, etc.), while in other situations the array functions as a set and is not order sensitive. The three formats proposed here are intended to cover all variants of array use cases.

Proposed formats

The base type for these formats is array.

set

A collection of unique, unordered items. The position of an item in the array is not significant.

Order need not be preserved in serialization, deserialization, compression, and transmission.

This format implies uniqueItems: true and is incompatible with uniqueItems: false.

multiset

A collection of unordered items allowing for duplicates. The position of an item in the array is not significant.

Order need not be preserved in serialization, deserialization, compression, and transmission.

This format implies uniqueItems: false and is incompatible with uniqueItems: true.

sequence

An ordered collection of items. The order is defined by each item's index in the array, and not by comparing the items' values.

Order must be preserved in serialization, deserialization, compression, and transmission.

This format is compatible with any value for uniqueItems.

Alternative names for this format were considered, but were deemed less suitable:

  • list: too vague and overloaded
  • series: in mathematics, a series is order insensitive
  • tuple, vector: these imply a fixed length

Notes

These formats do not alter the definition of item uniqueness, which comes from the JSON Schema definition of instance equality. Note that uniqueItems does not consider semantic equivalence. For example, date-time equivalency is not considered here:

ExampleTimes:
  type: array
  uniqueItems: true
  items:
    type: string
    format: date-time
  example:
    - '1996-12-19T16:39:57-08:00'
    - '1996-12-20T00:39:57Z'

The set and multiset formats are incompatible with conflicting uniqueItems values, as described above. Existing formats have similar incompatibilities. For example, string-based formats like date and email are incompatible with conflicting pattern values.

The multiset format is included for completeness. The data type it represents does not appear to be commonly used.

The sequence and set formats represent data types that are very common in real-world API use. For example, the Terraform types include:

  • list (or tuple): a sequence of values, like ["us-west-1a", "us-west-1c"]. Identify
    elements in a list with consecutive whole numbers, starting with zero.
  • set: a collection of unique values that do not have any secondary identifiers or ordering.

Having the proposed formats enables Terraform providers to correctly make use of the above Terraform types.

Examples

set

Album:
  properties:
    name:
      type: string
    genres:
      type: array
      format: set
      items:
        type: string
      example: [jazz, rock]  # equivalent to [rock, jazz]

multiset

Survey:
  properties:
    question:
      type: string
      example: How often do you exercise?
    collected_responses:
      type: array
      format: multiset
      items:
        type: string
      example:
      - "Daily"
      - "Once or twice a week"
      - "Daily"
      - "Every month"
      example2:  # equivalent to the above example
      - "Daily"
      - "Daily"
      - "Every month"
      - "Once or twice a week"

sequence

NetworkConfiguration:
  properties:
    dns_servers:
      type: array
      uniqueItems: true
      format: sequence  # Order is significant. The first server will be queried first.
                        # The next server will be used only when the first server fails.
      items:
        type: string
        format: ipv4
      example:
      - 192.168.0.3
      - 192.168.0.2
@lornajane
Copy link
Contributor

Is the expectation that by adding these to the OpenAPI specification, all tools must implement all of them? That's quite a big impact and the benefits beyond what we already have in arrays isn't clear to me (yet).

I'd be interested to see examples of tools that already support this type of thing and how that's working out before we tried to make it official.

@meem
Copy link

meem commented Mar 6, 2024

@lornajane Per https://spec.openapis.org/registry/format/, tools are not required to implement formats in the registry.

@nfroidure
Copy link
Contributor

Would be a nice way to solve that long lasting issue : #883

🤞

@hkosova
Copy link
Contributor

hkosova commented May 2, 2024

@HenryGessau I think your proposed sequence format is already covered by the prefixItems keyword available in OpenAPI 3.1.

Example from here:

# 2-element tuple; the 1st element is a number, the 2nd element is a string

type: array
prefixItems:

  # The 1st item
  - type: integer
    description: Description of the 1st item

  # The 2nd item
  - type: string
    description: Description of the 2nd item

  # Define the 3rd etc. items if needed
  # ...

# The total number of items in this tuple - in case it needs to be limited
minItems: 2
maxItems: 2
additionalItems: false   # can be omitted if `maxItems` is specified

@LasneF
Copy link
Member

LasneF commented Oct 28, 2024

🤔isn't more a requirement toward JsonSchema rather than OAS ?

@handrews
Copy link
Member

@LasneF it has previously been on the TDC agenda to determine whether we should close JSON Schema-related requests (I am in favor of doing so), but we have never managed to make an actual decision on it.

It is actually part of our issue tracking the need to document issue closure criteria:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants