question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Undesired property injected in the generated response schema of array type

See original GitHub issue

I noticed that a property maxItems: 2 was injected to the generated response schema despite not being present in the original definition. I tried to narrow down the issue and arrived at a trimmed version of the Swagger Petshop.

Giving this as input to portman (both files are attached):

> portman -l petshop-api-bug.yaml -o ./petshop-api-bug-postman.json
======================================
 Local Path:            petshop-api-bug.yaml
 Output Path:           ./petshop-api-bug-postman.json
 Portman Config:        portman-config.default.json
 Postman Config:        postman-config.default.json
 Environment:           .env
 Inject Tests:          true
 Run Newman:            false
 Newman Iteration Data: false
 Upload to Postman:     false  
======================================
  ✔ Conversion successful
======================================
🚀 Collection written to: ./petshop-api-bug-postman.json 🚀
======================================

While successful, it generates this schema (pretty printed):

// Response Validation
const schema = {
  "type": "array",
  "items": {
    "required": [
      "name"
    ],
    "properties": {
      "id": {
        "type": "integer",
        "example": 10
      },
      "name": {
        "type": "string",
        "example": "doggie"
      },
      "status": {
        "type": "string",
        "description": "pet status in the store",
        "enum": [
          "available"
        ]
      }
    },
    "type": "object"
  },
  "maxItems": 2
}

Notice the last property.

Possible cause and a workaround

If you look at response test generation code, you can see that traverse in convertUnsupportedJsonSchemaProperties explicitly checks for and deletes minItems: 2 and maxItems: 2. A comment reveals another part of the conversion is known to inject these unwanted properties.

The branching condition, specificallyobj[k]?.type === 'array', hints that this is is only an issue when the type is set to array. However, if the schema has an array as the outermost type, for instance

schema:
  type: array
  items: 
    $ref: '#components/MyItems'

as in my example, then we’d never check for f the first loop immediately iterates over the keys type and items.

The workaround

If my guess of the cause is correct, just changing the schema to begin with an object instead of an array type should be enough to make traverse see the array and delete the maxItems field.

Modified response schema in petshop-api-bug.yaml:

schema:
    type: object
    required:
      - pets
    properties:
      pets:
        type: array
        items:
          $ref: '#/components/schemas/Pet'

And this works. The generated schema is now:

{
  "type": "object",
  "required": [
    "pets"
  ],
  "properties": {
    "pets": {
      "type": "array",
      "items": {
        "required": [
          "name"
        ],
        "properties": {
          "id": {
            "type": "integer",
            "example": 10
          },
          "name": {
            "type": "string",
            "example": "doggie"
          },
          "status": {
            "type": "string",
            "description": "pet status in the store",
            "enum": [
              "available"
            ]
          }
        },
        "type": "object"
      }
    }
  }
}

Versions

> portman --version
1.8.0

On Ubuntu 20.04 LTS running under WSL.

Attachments

petshop-api-bug.zip

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
thim81commented, Aug 30, 2021

@adnilsson We have a fix ready. I’ll add some more tests to include it in our test coverage and put it in a PR.

2021-08-30 at 16 27 41@2x

The generated JSON schema result would be:

{
   "type":"array",
   "items":{
      "required":[
         "name"
      ],
      "properties":{
         "id":{
            "type":"integer",
            "example":10
         },
         "name":{
            "type":"string",
            "example":"doggie"
         },
         "status":{
            "type":"string",
            "description":"pet status in the store",
            "enum":[
               "available"
            ]
         }
      },
      "type":"object"
   }
}

The fix will most likely land in the next release.

0reactions
thim81commented, Sep 21, 2021

@jonathanevans-freesat Thanks for reporting it. Something went wrong with the build, so apologies for the inconvenience. Going to close the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[JAVA][SPRING] Arrays are not rendered correctly in responses
Description Objects of type "array" are not rendered correctly in responses, see example below. The UI shows only an item of the array,...
Read more >
JSON Schema for array of different types with mandatory ...
In this case, it simply isn't required, and you can put properties and additionalProperties straight in the root schema.
Read more >
Getting started with Schema Registry - AWS Glue
When your registry is created it is assigned an Amazon Resource Name (ARN), which you can view by choosing the registry from the...
Read more >
JSON-Schema everywhere. How to make web development ...
The parameter 'CustomerOrderSchema' provided to our middleware comes from the $id property of our schema. You can identify a schema, or a part...
Read more >
Appendices - Micronaut Documentation
Micronaut supports the following types of dependency injection: Constructor injection (must be ... Specifying lists or arrays in Java properties using index.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found