OpenAPI Specification Schemas: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 51: Line 51:
   type: object
   type: object
   properties:
   properties:
     color:  
     color: # 'color' is the name of a field of MyType
       type: string
       type: string
     size:
     size: # 'size' is the name of a field of MyType
       type: integer
       type: integer
       format: int32
       format: int32
     weight:
     weight: # 'weight' is the name of a field of MyType
       type: number
       type: number
       format: double
       format: double
</syntaxhighlight>
</syntaxhighlight>
==<tt>required</tt>==
==<tt>required</tt>==



Revision as of 02:33, 26 January 2024

External

Internal

Overview

The /components/schemas section of the OpenAPI specification defines reusable types that are used as input and output data types. These types can represent objects, but also primitives and arrays. The specification is based on JSON Schema Specification Draft 2020-12. A client or server code generator creates programming language types from these schemas.

This article is annotated with details related to how oapi-codegen generates Go code.

Schema

SchemaName
  type:
  properties:
    color:
      type: string
    size:
      type: integer
      format: int32
    weight:
      type: number
      format: double
  required:
    - color
    - size
    - weight
  format:
  description:
  discriminator:
  externalDocs:

A Go type is generated for a schema only if a schema is referred from the specification with schema.$ref. If the type is declared, but not referred, no code will be generated for it.

The types and the fields are implicitly exported with oapi-codegen, they start with upper case characters, even if they are declared with lower case characters in the OpenAPI specification.

Schema Name

type

One of the supported data types.

properties

For a schema of type object, properties contains a map whose keys are the names of the fields. Each map element must mandatorily include type and optionally format:

MyType:
  type: object
  properties:
    color: # 'color' is the name of a field of MyType
      type: string
    size: # 'size' is the name of a field of MyType
      type: integer
      format: int32
    weight: # 'weight' is the name of a field of MyType
      type: number
      format: double

required

An array with the name of the required fields:

MyType:
  [...]
  required:
    - color
    - size
    - weight

If a field is declared as "required", oapi-codegen will generate the struct corresponding to the schema with a value for that field. If the field is not among the required fields, the struct will carry a pointer instead of a the value, presumably so it can be set to nil.

format

Example

components:
  schemas:
    Pet:
      allOf:
        - $ref: '#/components/schemas/NewPet'
        - type: object
          required:
            - id
          properties:
            id:
              type: integer
              format: int64
    NewPet:
      type: object
      required:
        - name
      properties:
        name:
          type: string
        tag:
          type: string
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

Composition

https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#composition-and-inheritance-polymorphism

allOf

Polymorphism

https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#composition-and-inheritance-polymorphism

discriminator