[ADP-761] OpenAPI: public shared schemas
Guidance
- SHOULD publish stable schemas to a public site for reuse across projects/teams.
- MUST use semantic versioning for published schemas as outlined in ADP-24.
- SHOULD document all published schemas thoroughly, including descriptions for each field.
- MUST ensure backward compatibility when updating published schemas, or increment the major version if breaking changes are necessary.
What Deserves to Be Published?
- ProblemDetail Schema: Standardized schema for representing HTTP problem details.
- ErrorResponse Schema: Common schema for API error responses.
- ErrorResponseExample Schema: Example schemas for different error responses.
- CloudEvent Schema: Schema for CloudEvents as defined by the CNCF.
- Pagination Schema: Common schema for paginated responses.
- SortOrder Schema: Schema for defining sort order in requests.
- Filter Schema: Generic schema for filtering requests.
What Schema Is Not Recommended to Be Published?
- Object Schema:
- These schemas change too frequently.
- If you'd like to publish, use versioning to manage updates and ensure backward compatibility.
Benefits of Shared Schemas
- Consistency: Using shared schemas ensures that common data structures are uniform across different APIs.
- Maintainability: Centralized management of schemas simplifies updates and changes.
- Efficiency: Reduces duplication of effort in defining and documenting common structures.
- Interoperability: Promotes easier integration between different services and systems.
Implementation Details
Publishing Process
- Review and approve the schema within your team.
- Version the schema according to ADP-24.
- Publish the schema to a designated public repository or API registry.
- Update documentation to reflect the new or updated schema.
Referencing Published Schemas
When referencing a published schema, use the full URL to the specific version:
yaml
components:
schemas:
Error:
$ref: 'https://api.example.com/schemas/v1/Error.yaml'
Examples
Problem Details (HTTP Problem)
yaml
ProblemDetails:
type: object
properties:
type:
type: string
format: uri
title:
type: string
status:
type: integer
format: int32
detail:
type: string
instance:
type: string
format: uri
required:
- type
- title
- status
- detail
CloudEvents
yaml
CloudEvent:
type: object
properties:
specversion:
type: string
enum: ["1.0"]
type:
type: string
source:
type: string
format: uri
id:
type: string
time:
type: string
format: date-time
datacontenttype:
type: string
data:
type: object
required:
- specversion
- type
- source
- id
Best Practices
- Keep schemas as simple and generic as possible to maximize reusability.
- Provide clear documentation for each schema, including usage examples.
- Establish a governance process for schema updates and versioning.
- Regularly review and update shared schemas based on feedback and evolving requirements.
- Consider creating language-specific bindings or code generation tools for popular programming languages.
Related ADPs
- ADP-24: Version Control Mechanism
- ADP-758: Use of $ref in OpenAPI
- ADP-401: HTTP Problem Basics
- ADP-602: CloudEvents