openapi: 3.1.0
info:
  title: OHN-21 FHIR Interoperability Gateway
  version: 1.0.1
  description: |
    Governed FHIR R4 gateway. All operations need a bearer token carrying role, jurisdiction, organization and purpose of use;
    default deny. Paths under /R4 answer with FHIR resources (OperationOutcome on every failure). Every POST needs an
    Idempotency-Key. Synthetic data only until upstream services (OHN-16, 18, 23, 39, 41) are accepted.

    Tokens come from the platform gateway (ES256, audience ohn-21); the TypeScript-only synthetic sign-in (POST /synthetic-token) is not served by the Java service and is not part of this contract.
    A query string is strict: an unknown or repeated key, or a bad value, is 422 domain_validation_failed. Error bodies are the platform problem+json superset of {error, message, details}; on /R4 paths every
    failure is an OperationOutcome whose issue[0].details.coding[0].code is the machine error and whose extension urn:ohn:ohn-21:details carries the details as JSON text.
servers:
  - url: https://opeshealthnetwork.com
security:
  - bearerAuth: []
paths:
  /health/ohn-21:
    get:
      operationId: ohn21_health
      summary: Liveness and dependency mode
      security: []
      responses:
        '200': { description: Healthy }
        '503': { description: Database unavailable }
  /api/ohn/v1/fhir/R4/metadata:
    get:
      operationId: ohn21_metadata
      x-inventory-id: OHN-21-API-001
      summary: Published CapabilityStatement (FHIR metadata)
      responses:
        '200': { description: Success, content: { application/fhir+json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/FhirError' }
        '401': { $ref: '#/components/responses/FhirError' }
        '403': { $ref: '#/components/responses/FhirError' }
        '404': { $ref: '#/components/responses/FhirError' }
        '409': { $ref: '#/components/responses/FhirError' }
        '410': { $ref: '#/components/responses/FhirError' }
        '422': { $ref: '#/components/responses/FhirError' }
        '429': { $ref: '#/components/responses/FhirError' }
        '503': { $ref: '#/components/responses/FhirError' }
  /api/ohn/v1/fhir/R4:
    post:
      operationId: ohn21_transaction
      x-inventory-id: OHN-21-API-002
      summary: Process an atomic FHIR transaction Bundle (W001, W002)
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody: { required: true, content: { application/fhir+json: { schema: { $ref: '#/components/schemas/FhirBundle' } } } }
      responses:
        '200': { description: Success, content: { application/fhir+json: { schema: { type: object } } } }
        '202': { description: Held for reconciliation (upstream unavailable), content: { application/fhir+json: { schema: { $ref: '#/components/schemas/OperationOutcome' } } } }
        '400': { $ref: '#/components/responses/FhirError' }
        '401': { $ref: '#/components/responses/FhirError' }
        '403': { $ref: '#/components/responses/FhirError' }
        '404': { $ref: '#/components/responses/FhirError' }
        '409': { $ref: '#/components/responses/FhirError' }
        '410': { $ref: '#/components/responses/FhirError' }
        '422': { $ref: '#/components/responses/FhirError' }
        '429': { $ref: '#/components/responses/FhirError' }
        '503': { $ref: '#/components/responses/FhirError' }
  /api/ohn/v1/fhir/R4/Patient/{id}:
    get:
      operationId: ohn21_readPatient
      x-inventory-id: OHN-21-API-003
      summary: Read a Patient after routing and OHN-16 PDP permit
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, format: uuid } }
      responses:
        '200': { description: Success, content: { application/fhir+json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/FhirError' }
        '401': { $ref: '#/components/responses/FhirError' }
        '403': { $ref: '#/components/responses/FhirError' }
        '404': { $ref: '#/components/responses/FhirError' }
        '409': { $ref: '#/components/responses/FhirError' }
        '410': { $ref: '#/components/responses/FhirError' }
        '422': { $ref: '#/components/responses/FhirError' }
        '429': { $ref: '#/components/responses/FhirError' }
        '503': { $ref: '#/components/responses/FhirError' }
  /api/ohn/v1/fhir/R4/$validate:
    post:
      operationId: ohn21_validate
      x-inventory-id: OHN-21-API-004
      summary: Validate a resource against a profile pack (OHN-21-F003-SUBMIT)
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody: { required: true, content: { application/fhir+json: { schema: { $ref: '#/components/schemas/ValidateParameters' } } } }
      responses:
        '200': { description: Success, content: { application/fhir+json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/FhirError' }
        '401': { $ref: '#/components/responses/FhirError' }
        '403': { $ref: '#/components/responses/FhirError' }
        '404': { $ref: '#/components/responses/FhirError' }
        '409': { $ref: '#/components/responses/FhirError' }
        '410': { $ref: '#/components/responses/FhirError' }
        '422': { $ref: '#/components/responses/FhirError' }
        '429': { $ref: '#/components/responses/FhirError' }
        '503': { $ref: '#/components/responses/FhirError' }
  /api/ohn/v1/fhir/context:
    get:
      operationId: ohn21_context
      summary: Caller context and capability flags
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/vocabulary:
    get:
      operationId: ohn21_vocabulary
      summary: Controlled vocabulary
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/routes:
    get:
      operationId: ohn21_listRoutes
      summary: List FHIR routes
      parameters:
        - { name: state, in: query, required: false, schema: { $ref: '#/components/schemas/RouteState' } }
        - { name: organization_id, in: query, required: false, schema: { type: string, format: uuid } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
    post:
      operationId: ohn21_createRoute
      summary: Create a FHIR integration route (OHN-21-F001-SUBMIT)
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/RouteCreate' } } } }
      responses:
        '201': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/routes/{id}:
    get:
      operationId: ohn21_getRoute
      summary: Route detail with history, policies and validation runs
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, format: uuid } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/routes/{id}/verify-trust:
    post:
      operationId: ohn21_verifyTrust
      summary: W003 partner_trust_verified (OHN-39, separation of duties)
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, format: uuid } }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/Versioned' } } } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/routes/{id}/check-profiles:
    post:
      operationId: ohn21_checkProfiles
      summary: W003 profiles_checked (OHN-41)
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, format: uuid } }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/Versioned' } } } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/routes/{id}/test:
    post:
      operationId: ohn21_testRoute
      summary: W003 tested (needs passing strict validation runs)
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, format: uuid } }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/Versioned' } } } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/routes/{id}/enable:
    post:
      operationId: ohn21_enableRoute
      summary: W003 enabled (writes routing policy; refuses profile drift)
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, format: uuid } }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/Versioned' } } } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/routes/{id}/disable:
    post:
      operationId: ohn21_disableRoute
      summary: Disable a route and retire its routing policy
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, format: uuid } }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/Disable' } } } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/routing-policies:
    get:
      operationId: ohn21_listRoutingPolicies
      summary: List routing policy snapshots
      parameters:
        - { name: status, in: query, required: false, schema: { enum: [active, retired] } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/validation-runs:
    get:
      operationId: ohn21_listValidationRuns
      summary: List validation runs (content references only)
      parameters:
        - { name: route_id, in: query, required: false, schema: { type: string, format: uuid } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/transactions:
    get:
      operationId: ohn21_listTransactions
      summary: List FHIR transactions
      parameters:
        - { name: status, in: query, required: false, schema: { $ref: '#/components/schemas/TransactionState' } }
        - { name: organization_id, in: query, required: false, schema: { type: string, format: uuid } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/transactions/{id}:
    get:
      operationId: ohn21_getTransaction
      summary: Transaction detail with retries and rollback evidence
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, format: uuid } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/transactions/{id}/retry:
    post:
      operationId: ohn21_retryTransaction
      summary: Retry a held, rejected or rolled-back transaction (OHN-21-F002-SUBMIT)
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, format: uuid } }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/Retry' } } } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/capability-statements:
    get:
      operationId: ohn21_listCapabilityStatements
      summary: Published CapabilityStatement versions
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/capability-statements/preview:
    get:
      operationId: ohn21_previewCapabilityStatement
      summary: Draft CapabilityStatement compared with the published one
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/capability-statements/publish:
    post:
      operationId: ohn21_publishCapabilityStatement
      summary: Reviewer decision on the draft CapabilityStatement
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/Publish' } } } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/audit:
    get:
      operationId: ohn21_audit
      summary: Audit trail (history timeline)
      parameters:
        - { name: aggregate_id, in: query, required: false, schema: { type: string, format: uuid } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/reports/{reportId}:
    get:
      operationId: ohn21_runReport
      summary: Run report OHN-21-R001..R003
      parameters:
        - { name: partner, in: query, required: false, schema: { type: string, format: uuid } }
        - { name: resource, in: query, required: false, schema: { type: string, maxLength: 64 } }
        - { name: rule, in: query, required: false, schema: { type: string, maxLength: 32 } }
        - { name: from, in: query, required: false, schema: { type: string, format: date-time } }
        - { name: to, in: query, required: false, schema: { type: string, format: date-time } }
        - { name: reportId, in: path, required: true, schema: { enum: [OHN-21-R001, OHN-21-R002, OHN-21-R003] } }
      responses:
        '200': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/reports/{reportId}/export:
    post:
      operationId: ohn21_exportReport
      summary: Create an audited CSV/PDF export
      parameters:
        - { name: reportId, in: path, required: true, schema: { enum: [OHN-21-R001, OHN-21-R002, OHN-21-R003] } }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/Export' } } } }
      responses:
        '201': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/exports/{token}:
    get:
      operationId: ohn21_downloadExport
      summary: Download an export (owner only, 15 minutes)
      parameters:
        - { name: token, in: path, required: true, schema: { type: string, pattern: '^[0-9a-f]{32}$' } }
      responses:
        '200': { description: 'The export body (not single use; 15 minutes; owner only)', content: { text/csv: { schema: { type: string } }, application/pdf: { schema: { type: string, format: binary } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
  /api/ohn/v1/fhir/synthetic/fixtures:
    post:
      operationId: ohn21_syntheticFixtures
      summary: 'Synthetic mode only: configure test fakes (404 otherwise)'
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/SyntheticFixtures' } } } }
      responses:
        '201': { description: Success, content: { application/json: { schema: { type: object } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
        '410': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
components:
  securitySchemes:
    bearerAuth: { type: http, scheme: bearer, bearerFormat: JWT }
  parameters:
    IdempotencyKey: { name: Idempotency-Key, in: header, required: true, schema: { type: string, minLength: 8, maxLength: 512, pattern: '^[A-Za-z0-9_.:-]+$' } }
  responses:
    Error: { description: Error, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    FhirError: { description: OperationOutcome, content: { application/fhir+json: { schema: { $ref: '#/components/schemas/OperationOutcome' } } } }
  schemas:
    FhirVersion: { enum: ['4.0.1'] }
    ResourceType: { enum: [Patient, Observation, Condition, Encounter, Bundle] }
    Operation: { enum: [Patient.read, Patient.create, Patient.update, Observation.read, Observation.create, Observation.update, Condition.read, Condition.create, Condition.update, Encounter.read, Encounter.create, Encounter.update, Bundle.transaction] }
    ConformanceMode: { enum: [strict, lenient] }
    ReplayScope: { enum: [full_bundle, revalidate_only] }
    RouteState: { enum: [route_draft, partner_trust_verified, profiles_checked, tested, enabled, disabled] }
    TransactionState: { enum: [received, rejected, committed, rolled_back, pending_reconciliation] }
    Error:
      type: object
      description: Platform problem+json superset of the reference's {error, message, details}
      properties: { error: { type: string }, message: { type: string }, details: { description: 'validation: {formErrors: [string], fieldErrors: {field: [string]}}; refusals: a small object (missing, trust_status, ...)' }, type: { type: string }, title: { type: string }, status: { type: integer }, detail: { type: string }, trace_id: { type: string } }
    OperationOutcome:
      type: object
      required: [resourceType, issue]
      properties:
        resourceType: { const: OperationOutcome }
        id: { type: string }
        issue: { type: array, items: { type: object, properties: { severity: { enum: [error, warning, information] }, code: { type: string }, diagnostics: { type: string }, expression: { type: array, items: { type: string } } } } }
    FhirBundle:
      type: object
      required: [resourceType, type, entry]
      properties: { resourceType: { const: Bundle }, type: { const: transaction }, entry: { type: array, items: { type: object } } }
    ValidateParameters:
      description: FHIR Parameters with resource, profile (profile pack uuid), ohn-source-endpoint, ohn-resource-type, ohn-conformance-mode. OHN-21-F003-SUBMIT.
      type: object
      required: [resourceType, parameter]
      properties: { resourceType: { const: Parameters }, parameter: { type: array, items: { type: object } } }
    RouteCreate:
      description: OHN-21-F001-SUBMIT
      type: object
      additionalProperties: false
      required: [organization_id, base_url, fhir_version, allowed_operations, profile_pack_ref, certificate_ref]
      properties:
        organization_id: { type: string, format: uuid }
        base_url: { type: string, format: uri, maxLength: 2048, pattern: '^https://' }
        fhir_version: { $ref: '#/components/schemas/FhirVersion' }
        allowed_operations: { type: string, minLength: 1, maxLength: 8192, description: 'Operation values separated by commas, spaces or new lines; at least one, each from the Operation list' }
        profile_pack_ref: { type: string, format: uuid }
        certificate_ref: { type: string, format: uuid }
    Retry:
      description: OHN-21-F002-SUBMIT
      type: object
      additionalProperties: false
      required: [transaction_id, retry_reason, idempotency_key, replay_scope, expected_version]
      properties:
        transaction_id: { type: string, format: uuid }
        retry_reason: { type: string, minLength: 10, maxLength: 4096 }
        idempotency_key: { type: string, minLength: 8, maxLength: 512, pattern: '^[A-Za-z0-9_.:-]+$', description: Must equal the Idempotency-Key header }
        replay_scope: { $ref: '#/components/schemas/ReplayScope' }
        expected_version: { type: integer, minimum: 1 }
    Versioned:
      type: object
      additionalProperties: false
      required: [expected_version]
      properties: { expected_version: { type: integer, minimum: 1 } }
    Disable:
      type: object
      additionalProperties: false
      required: [expected_version, reason]
      properties: { expected_version: { type: integer, minimum: 1 }, reason: { type: string, minLength: 10, maxLength: 4096 } }
    Publish:
      type: object
      additionalProperties: false
      required: [decision, reason, expected_current_version, preview_hash]
      properties: { decision: { enum: [approve, reject] }, reason: { type: string, minLength: 10, maxLength: 4096 }, expected_current_version: { type: integer, minimum: 0 }, preview_hash: { type: string, pattern: '^[0-9a-f]{64}$' } }
    Export:
      type: object
      additionalProperties: false
      required: [format]
      properties: { format: { enum: [csv, pdf] }, filters: { $ref: '#/components/schemas/ReportFilters' } }
    ReportFilters:
      type: object
      additionalProperties: false
      description: Strict; every value a string of at most 128 characters on the wire, then checked by its own rule
      properties: { partner: { type: string, format: uuid }, resource: { type: string, maxLength: 64 }, rule: { type: string, maxLength: 32 }, from: { type: string, format: date-time }, to: { type: string, format: date-time } }
    SyntheticFixtures:
      description: Synthetic mode only (ohn.m21.synthetic-gateways=true); 404 otherwise. Strict.
      type: object
      additionalProperties: false
      properties:
        certificate: { type: object, additionalProperties: false, required: [certificate_ref, organization_id, base_url, status], properties: { certificate_ref: { type: string, format: uuid }, organization_id: { type: string, format: uuid }, base_url: { type: string, format: uri }, status: { enum: [verified, unverified, revoked, expired] } } }
        patient_permit: { type: object, additionalProperties: false, required: [patient_id, actor_id, purpose], properties: { patient_id: { type: string, pattern: '^[A-Za-z0-9\\-.]{1,64}$' }, actor_id: { type: string, format: uuid }, purpose: { enum: [integration_operations, partner_assurance, treatment] } } }
        outage: { type: object, additionalProperties: false, required: [port, down], properties: { port: { enum: [pdp, audit, terminology, trust, repository] }, down: { type: boolean } } }
        fail_commit_at: { type: integer, minimum: -1, maximum: 1000 }
        profile_pack_bump: { type: string, format: uuid }
