Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenAPI 3 - readOnly property, but allow write in POST/PUT

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"}
like image 810
crunk1 Avatar asked May 16 '26 13:05

crunk1


1 Answers

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
like image 134
Helen Avatar answered May 19 '26 04:05

Helen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!