7

Is there a way to represent a property as generally readOnly, but allow the property to be written during a POST or PUT (i.e. a create or replace) action?

In other words, when creating the resource, I'd like the property to be writable. But once the resource is created, I'd like to keep it immutable. Can a property be POSTable/PUTable, but not PATCHable?

Example:

# OK example.
/AwesomeResource POST
{"owner": "ownerID123"}

vs

# Bad Request example.
/AwesomeResource/456 PATCH
{"owner": "ownerID789"}
crunk1
  • 2,560
  • 1
  • 26
  • 32

1 Answers1

10

You'll need separate models for POST/PUT and PATCH/GET. The POST/PUT model with writable owner property can be the base model, which the PATCH/GET model will extend by adding the readOnly constraint.

# openapi: 3.0.0

components:
  schemas:

    # Model for POST and PUT
    NewAwesomeResource:
      type: object
      properties:
        owner:
          type: string
          example: ownerID789
        ...

    # Model for PATCH and GET
    AwesomeResource:
      allOf:
        - $ref: '#/components/schemas/NewAwesomeResource'
      properties:
        owner:
          readOnly: true
Craigo
  • 3,384
  • 30
  • 22
Helen
  • 87,344
  • 17
  • 243
  • 314