mrtnvh.com

Frontend ♥︎
OpenAPI

by mrtnvh

Maarten Van Hoof

iO digital
  • Senior Front end Developer
  • Open Source Advocate
  • OpenAPI Advocate
Monolith application architecture
Headless application architecture
Headless application architecture with multiple clients
Headless application architecture with a code-first OpenAPI document as contract

OpenAPI

The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to RESTful APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection.

When properly defined, a consumer can understand and interact with the remote service with a minimal amount of implementation logic.

https://github.com/OAI/OpenAPI-Specification/

OpenAPI

Document layout


        openapi:
           # Specification version
        
        info:
           # General API metadata
        
        servers:
           # Server information. BaseURLs, environments, ...
        
        paths:
           # Available paths & operations
        
        components:
           # Datamodel abstraction
        
        security:
           # Security mechanisms can be used across the API
        
        tags:
           # Grouping of paths & components
        
        external docs:
           # Additional external documentation
        
        webhooks:
           # Webhook operations, similar to paths, only API is now a consumer.
        
        

OpenAPI

Paths


        paths:
        
          /pets:
            get:
              summary: List all pets
              #...
            post:
              summary: Create a pet
              #...
        
          /pets/{petId}:
            get:
              summary: Info for a specific pet
              #...
        
        

OpenAPI

Operation


        paths:
          /pets:
            get:
              summary: List all pets
              operationId: listPets
              tags:
                - pets
              parameters:
                - name: limit
                  in: query
                  description: How many items to return at one time
                  required: false
                  schema:
                    type: integer
                    format: int32
              responses:
                #...
        
              # description
              # requestBody
              # externalDocs
              # deprecated
              # security
              # servers
              # ...
        

OpenAPI

Response


        paths:
          /pets:
            get:
              #...
              responses:
                '200':
                  description: Expected response to a valid request
                  content:
                    application/json:
                      schema:
                        $ref: "#/components/schemas/Pets"
                default:
                  description: unexpected error
                  content:
                    application/json:
                      schema:
                        $ref: "#/components/schemas/Error"
        

OpenAPI

Components


        components:
          schemas:
            Pet:
            Pets:
        
          responses:
            ErrorResponse:
        
          requestBodies:
            NewPet:
        
          headers:
            Limit:
            Offset:
            Pagination:
        
          #...
        

OpenAPI

Component


        components:
          schemas:
            Pet:
              type: object
              properties:
                id:
                  type: integer
                  format: int64
                name:
                  type: string
                  x-custom-extension: name.firstName
                image:
                  type: string
                tag:
                  type: string
              required:
                - id
                - name
        
            Pets:
              type: array
              items:
                $ref: "#/components/schemas/Pet"
        

Want to learn more?

spec.learnopenapi.com

Code first

Headless application architecture with a code-first OpenAPI document as contract

Code first

Generate OpenAPI document from API framework

Java springdoc-openapi
.NET SwashBuckle, Nswag
NodeJS: @nestjs/swagger

Design first

Headless application architecture with a code-first OpenAPI document as contract

Design first

OpenAPI doc = single source of truth

Entire team can be involved in developing the communication layer.

  • Providers
  • Consumers
  • Project owner: future implementation, roadmap considerations

Tooling

Harness the power

Documentation

Generate entire API documentation

Less technical representation

Tools:

Linting

Quality assurance for OpenAPI documents

  • Validations
  • Formatting

Benefits

  • Reduce decision-making
  • Creates consistency

 

Spectral (by Stoplight)

SDK Generation

Headless application architecture with a code-first OpenAPI document as contract

SDK Generation

Headless application architecture with clients in multiple programming languages

SDK Generation

Headless application architecture with clients in multiple programming languages and a GraphQL server

SDK Generation

Mocking

Turn OpenAPI files into a API servers with mocking, transformations, validations, ...


        # ...
        paths:
          /pets:
            get:
              operationId: listPets
              responses:
                "200":
                  content:
                    application/json:
                      schema:
                        type: array
                        items:
                          type: object
                          properties:
                            id:
                              type: integer
                              format: int64
                            name:
                              type: string
        # ...
        

Prism (by Stoplight)

Mocking

Turn OpenAPI files into a API servers with mocking, transformations, validations, ...


        [
          {
            "id": 7094252654184530000,
            "name": "proident consequat anim est elit",
            "tag": "eiu"
          },
          {
            "id": 2067454767236280300,
            "name": "nostrud cupidatat nisi f",
            "tag": "sed eiusmod"
          },
          {
            "id": 5983151522463482000,
            "name": "dolore id minim magna",
            "tag": "aute consectetur do"
          }
        ]
        

Prism (by Stoplight)

Automation

OpenAPI doc = Single Source Truth

  • Quality assurance on Pull or Merge request
  • Instant documentation updates
  • SDK generation at new release/commit
  • Mocking server updates

 

TIL

OpenAPI specification
  • Contract between services
  • Unified way of describing a REST API
  • Code first
  • Design first
Tools
  • Documentation generation
  • SDK generation
  • Linting
  • Mocking servers
  • Automation
Results
  • Single source of truth
  • Enhances workflow
  • Increases independence

FIN

Find me

mrtnvh.com@mrtnvh

iodigital.com
Peace