Fionn
Fionn

Reputation: 11265

Extended JSON Metadata with Swagger/OpenAPI

I’m searching for a way to declare extended metadata for JSON objects used in an API which is specified using Swagger/OpenAPI (or something similar if there is another format supporting the requested features).

The idea is to use this metadata to automatically/partially generate user interfaces for editing this data.

A list of the requested features:

Upvotes: 4

Views: 2377

Answers (1)

Arnaud Lauret
Arnaud Lauret

Reputation: 5281

The OpenAPI (fka. Swagger) specification can be extended by using x- properties (See Specification Extension is the specification). These x- properties are ignored by standard OpenAPI parsers.

You can define your own properties almost anywhere in the specification file even in JSON schema definitions, but you'll have to write your own parser to use them and/or modify tools such as Swagger UI (which is fairly easy to do) to see them in such tools.

Here's an example with some x-tensions in definitions:

swagger: "2.0"

info:
  version: 1.0.0
  title: X-tension example
  description: Using x- properties to extend the OpenAPI specification
  x-example: we can put x-tension almost anywhere in the specification

paths: {}

definitions:
  User:
    properties:
      id:
        type: string
      name:
        type: string
        maxLength: 50
        minLength: 10
        x-validation:
          multiLingualMessage:
            en: Name's length must be between <minLength> and <maxLength>
            fr: La longeur de Name doit être comprise entre <minLength> et <maxLength>
      friends:
        type: array
        description: An array of UserId
        items:
          type: string
          x-reference:
            type: User

This OpenAPI specification is considered valid by the editor: it ignores the x-properties.

This example is only an illustration of x- properties and do not intend to solve all use cases listed in the question.

Upvotes: 6

Related Questions