stoplightio / prism

Turn any OpenAPI2/3 and Postman Collection file into an API server with mocking, transformations and validations.

Home Page:https://stoplight.io/open-source/prism

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Management of Required and ReadOnly with Array does not work correctly

LasneF opened this issue · comments

Context

given an object with a readOnly required field, prism does not handle it properly as soon as the object is embbed into an array

prism mentionned that the readOnly paramater is mandatory even for POST operation
notice that this occurs only when object is in array, and works correctly otherwise (sample provided below)

This is the case for prism in proxy mode or in mock mode

Expected Behavior

no error should be raised

Possible Workaround/Solution

no real solution available looks a bug

Steps to Reproduce

given the API spec here containing 2 endpoints one with array one without

openapi: 3.1.0

paths:
  /dogs:
    post:
      requestBody:
        content:
          application/json:
            schema:
                $ref: '#/components/schemas/Dogs'
      responses:
        '200':
          description: nice Dog
  /dogSingle:
    post:
      requestBody:
        content:
          application/json:
            schema:
                $ref: '#/components/schemas/Dog'
      responses:
        '200':
          description: nice Dog

  
components:
  schemas:
    Dogs:
        type: object
        properties:
            dogs:
              type: array
              items:
                $ref : "#/components/schemas/Dog"
    
    Dog:
        type: object
        required:
            - id
            - name
        properties:
            id:
              readOnly: true
              type: string
            name:
              type: string

Dog is a simple object with id as readOnly and name as string
dogs is an array of Dog

doing

curl --location 'http://127.0.0.1:4011/dogSingle' \
--header 'Content-Type: application/json' \
--data '{
            "name": "scooby"
}'

is OK

but doing the test with the array endpoints

curl --location 'http://127.0.0.1:4011/dogs' \
--header 'Content-Type: application/json' \
--data '{
    "dogs": [
        {
            "name": "scooby"
        }
    ]
}
'

fails with the following

{
    "type": "https://stoplight.io/prism/errors#UNPROCESSABLE_ENTITY",
    "title": "Invalid request",
    "status": 422,
    "detail": "Your request is not valid and no HTTP validation response was found in the spec, so Prism is generating this error for you.",
    "validation": [
        {
            "location": [
                "body",
                "dogs",
                "0"
            ],
            "severity": "Error",
            "code": "required",
            "message": "Request body property dogs.0 must have required property 'id'"
        }
    ]
}

Environment

tested on windows with prism 5.1.0

Could also be an issue if Dog is a property of another object. Need to check this case as well.

@chohmann tested with Dog as a property of another Object the bug looks not there !

see below spec

  • /dogSingle + { "name" = "happy" } => no issue
  • /dogInABox => no issue { "inside" : { "name" : "alsoHappy" } } => no issue
  • /dogs => Bug + { "dogs" : [{"name" = "sad" }] =>bug report id as mandatory

if i send wrong payload such as a dog like this "nane" : "typo"

  • both dogSingle and dogInABox return name is mandatory
  • dogs return that name AND id is mandatory
openapi: 3.1.0

paths:
  /dogs:
    post:
      requestBody:
        content:
          application/json:
            schema:
                $ref: '#/components/schemas/Dogs'
      responses:
        '200':
          description: nice Dog
  /dogSingle:
    post:
      requestBody:
        content:
          application/json:
            schema:
                $ref: '#/components/schemas/Dog'
      responses:
        '200':
          description: nice Dog
  /dogInABox:
    post:
      requestBody:
        content:
          application/json:
            schema:
                $ref: '#/components/schemas/BoxDog'
      responses:
        '200':
          description: nice Dog


  
components:
  schemas:
    Dogs:
        type: object
        properties:
            dogs:
              type: array
              items:
                $ref : "#/components/schemas/Dog"
    
    Dog:
        type: object
        required:
            - id
            - name
        properties:
            id:
              readOnly: true
              type: string
            name:
              type: string
    BoxDog:
      type: object
      required: inside
      properties:
        inside :
            $ref : "#/components/schemas/Dog"