Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Management of Required and ReadOnly with Array does not work correctly #2336

Open
LasneF opened this issue Jul 28, 2023 · 5 comments
Open

Management of Required and ReadOnly with Array does not work correctly #2336

LasneF opened this issue Jul 28, 2023 · 5 comments

Comments

@LasneF
Copy link

LasneF commented Jul 28, 2023

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

@LasneF LasneF changed the title Error in handling Required and ReadOnly with Array does not work Management of Required and ReadOnly with Array does not work correctly Jul 28, 2023
@chohmann
Copy link
Contributor

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

@LasneF
Copy link
Author

LasneF commented Aug 4, 2023

@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"

@ilanashapiro
Copy link
Contributor

ilanashapiro commented Apr 17, 2024

Hi! I am working on this issue and I have a quick question. I have what I think is a working solution for non-tuple typed arrays (i.e., arrays where every elements conforms to a single schema, like:

objectsArray: {
          type: 'array',
          items: {
            type: 'object',
            required: ['id', 'name'],
            properties: {
              id: {
                readOnly: true,
                type: 'string'
              },
              name: {
                type: 'string'
              }
            }
          }
        },

However, as I continue trying to implement the fix for tuple-typing, it has proven to be much more difficult (i.e. multiple schema corresponding to individual array items, like:

objectsArray: {
          type: 'array',
          items: [
            { 
              type: 'object', 
              required: ['id1, 'name'], 
              properties: { id: { readOnly: true, type: 'string' }, name: { type: 'string' } } 
            },
            { 
              type: 'object', 
              required: ['address', 'title'2], 
              properties: { address: { readOnly: true, type: 'string' }, title: { type: 'string' } } 
            }
          ]
        },

@rattrayalex I can submit the solution for the non-tuple typed arrays in the next day or so -- would you like me to continue working on the tuple typed cases after this? Thanks!
(edit: happy to continue working on it, just want to make sure you can get at least a partial solution more quickly)

@rattrayalex
Copy link

Sure, a follow-on PR with the other fixes sounds like something we'd be happy to sponsor!

@ilanashapiro
Copy link
Contributor

sounds good and fortunately I was actually able to do it for tuple typed arrays as well, which is all in the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants