Api Management

Contract Processing

Overview of contract generation and processing for services and gateways.

Overview

Each service defines:

  • Its service contract, which specifies available endpoints.
  • Its gateway configuration, detailing how endpoints are exposed through gateways.

The goal is to automate the generation of:

  1. Service contracts for each gateway: A split/projection operation creates separate contracts for each gateway. These contracts are used for:
    • Clients to communicate with services via gateways.
  2. Gateway contracts combining all services: A merge operation consolidates contracts for all services exposed via a particular gateway. These contracts:
    • Configure the gateway itself.
    • Facilitate client communication, though direct service contracts/packages are preferred.

Example

Input: service contracts and gateway configurations

Two example services, svc-customers and svc-blog-posts, provide the following contracts and configurations:

svc-customers - contract
  GET /customers/{id}
  POST /pba/customers
  GET /internal/stats

svc-customers - GW config
  selfcare:
    endpointsPathPrefix: cust
    dtoPrefix: Customers.
    endpoints:
      GET /customers/{id}
  pba:
    endpointsPathPrefix: cust
    dtoPrefix: Customers.
    endpoints:    
      POST /pba/customers
  
svc-blog-posts - contracts
  GET /blog-posts
  POST /pba/blog-posts

svc-blog-posts - GW config
  selfcare:
    endpointsPathPrefix: bp
    dtoPrefix: BlogPosts.
    endpoints:
      GET /blog-posts
  pba:
    endpointsPathPrefix: bp
    dtoPrefix: BlogPosts.
    endpoints:    
      POST /pba/blog-posts

Result: generated client and gateway contracts

svc-customers - selfcare gw contract
  GET /cust/customers/{id}

svc-customers - pba gw contract
  POST /cust/pba/customers

svc-blog-posts - selfcare gw contract
  GET /bp/blog-posts

svc-blog-posts - pba gw contract
  POST /bp/pba/blog-posts
  
selfcare gw contract
  GET /cust/customers/{id}
  GET /bp/blog-posts

pba gw contract
  POST /cust/pba/customers
  POST /bp/pba/blog-posts

Collisions

To prevent naming or routing conflicts:

  1. Path Prefixes: Each service is exposed on the gateway using a unique path prefix.
    • Example: cust for svc-customers, bp for svc-blog-posts
  2. DTO Prefixes: Each service's DTOs are prefixed to ensure uniqueness.
    • Example: Customers.CustomerDto, BlogPosts.BlogPostDto

Processing

The automation and processing of contract splitting and merging are implemented in the repository:
API Contracts Backend Repository


Copyright © 2025. All rights reserved.