OpenAPITools / OpenAPITools/openapi-generator

[BUG][validator] In some instances the location reported for an error is truncated

Open
#12,325 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I was using the Java language generator and it was reporting a missing key at a location that
was perfectly fine. I then repeated it with just using the validator

openapi-generator-cli validate -i myspec.json

I don't know the most general form of the defect is but this stripped down and
tweaked Petstore example recreated the issue.

{
  "openapi": "3.0.2",
  "info": {
    "title": "Swagger Petstore - OpenAPI 3.0",
    "description": "This is a sample Pet Store Server based on the OpenAPI 3.0 specification.  You can find out more about\nSwagger at [http://swagger.io](http://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!\nYou can now help us improve the API whether it's by making changes to the definition itself or to the code.\nThat way, with time, we can improve the API in general, and expose some of the new features in OAS3.\n\nSome useful links:\n- [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)\n- [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)",
    "termsOfService": "http://swagger.io/terms/",
    "contact": { "email": "apiteam@swagger.io" },
    "license": {
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
    },
    "version": "1.0.11"
  },
  "servers": [{ "url": "/api/v3" }],
  "tags": [
    {
      "name": "pet",
      "description": "Everything about your Pets",
      "externalDocs": {
        "description": "Find out more",
        "url": "http://swagger.io"
      }
    }
  ],
  "paths": {
    "/pet": {
      "put": {
        "tags": ["pet"],
        "summary": "Update an existing pet",
        "description": "Update an existing pet by Id",
        "operationId": "updatePet",
        "requestBody": {
          "description": "Update an existent pet in the store",
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/Pet" }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Successful operation",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Pet" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Pet": {
        "type": "object",
        "properties": {
          "items": {
            "oneOf": [
              {
                "type": "integer"
              },
              {
                "type": "object",
                "properties": {
                  "collection": {
                    "type": "array"
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
}

Here's the test run where a missing items keyword is detected:

✗ openapi-generator-cli validate -i dist/petstore.json   
Validating spec (dist/petstore.json)
Errors:
        - attribute components.schemas.Pet.items is missing

But the location is wrong. The actual missing key is
components.schemas.Pet.properties.items.oneOf[1].properties.collection.items

The original error message tripped me up in our actual spec because I
couldn't see anything wrong at the reported location.

openapi-generator version

5.4.0

Suggest a fix

I was keying in on the OpenAPIDeserialzer.getSchema because it seems to recurse
without modifying the location parameter that is passed in. But I tested with a
simplified version of the schema above, and it worked correctly. I'll need to
step thru with the debugger more times.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the report with openapi-generator-cli validate -i dist/petstore.json, then inspect OpenAPIDeserialzer.getSchema and how its recursive calls build the location. Done means the validator reports the full missing-key path, including properties.items.oneOf[1].properties.collection.items, with a regression test for the supplied schema.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.