Redocly/redocly-cli

dependentRequired is valid json schema but does not pass linting and prevents component from being available in UI.

jmacelroy opened this issue · 5 comments

Describe the bug

Property dependentRequired is not expected here is reported when running the linter. I would expect for an OpenAPI 3.1.0 spec for this to lint and be reflected in the UI generated as it seems to be part of the supported JSON Schema dialect.T

To Reproduce
Steps to reproduce the behavior:

  1. Given this redocly.yaml file
# See https://redocly.com/docs/cli/configuration/ for more information.
extends:
  - recommended 
rules:
  no-unused-components: error
  no-server-example.com: off
  no-empty-servers: off
  security-defined: off
  info-license: off
  operation-4xx-response: off # https://phab.easypo.net/T72078 turn this back on when 4xx are added.
theme:
  openapi:
    htmlTemplate: ./docs/index.html
    theme:
      colors:
        primary:
          main: "#32329f"
    generateCodeSamples:
      languages:  # Array of language config objects; indicates in which languages to generate code samples.
        - lang: curl
  1. And this OpenAPI file(s) which is using schema example from https://json-schema.org/understanding-json-schema/reference/conditionals#dependentRequired
openapi: 3.1.0
info:
  version: 1.0.0
  title: This is only a test.
  description: testing
paths:
  '/test':
    post:
      summary: test use of dependentRequired 
      operationId: test
      responses:
        "200":
          description: "OK"
          content:
            application/json:
              schema:
                type: object
                properties:
                  name:
                    type: string
                  credit_card:
                    type: number
                  billing_address:
                    type: string
                required:
                - name
                dependentRequired:
                  credit_card:
                  - billing_address

  1. Run this command with these arguments... redocly ...
    `redocly lint openapi.yaml

  2. See error

❯ redocly lint openapi.yaml
(node:45032) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
validating openapi.yaml...
[1] openapi.yaml:27:17 at #/paths/~1test/post/responses/200/content/application~1json/schema/dependentRequired

Property `dependentRequired` is not expected here.

25 | required:
26 | - name
27 | dependentRequired:
28 |   credit_card:
29 |   - billing_address

Error was generated by the spec rule.


openapi.yaml: validated in 7ms

❌ Validation failed with 1 error.
run `redocly lint --generate-ignore-file` to add all problems to the ignore file.

Expected behavior

The lint should pass.

Logs

n/a

OpenAPI description

Included above.

Redocly Version(s)

1.12.2

Node.js Version(s)

21.6.2

Additional context

I have tried to double check that it's not just a formatting error but this looks to be correct according the the json schema doc and is not a linting error when running spectral

Hey @jmacelroy! Thanks for reporting!
Meanwhile you can suppress the error by generating the ignore file: redocly lint openapi.yaml --generate-ignore-file to pass the validation.

BTW, what do you mean by '...prevents component from being available in UI'? Is it just linter failing and not passing to the next step or you'd expect the dependentRequired key to somehow be rendered on the UI? If the latter, you should probably open an issue in the Redoc repository.

BTW, what do you mean by '...prevents component from being available in UI'? Is it just linter failing and not passing to the next step or you'd expect the dependentRequired key to somehow be rendered on the UI? If the latter, you should probably open an issue in the Redoc repository.

It is the latter. I'll open an issue in https://github.com/Redocly/redoc. is that correct?

@tatomyr the type is not defined here.. https://github.com/Redocly/redocly-cli/blob/main/packages/core/src/types/oas3_1.ts#L90

should probably be defined with something similar to NamedSchemas

export const Oas3_1Types: Record<Oas3_1NodeType, NodeType> = {
...
dependentRequired: 'NamedDependents'
};

export const Oas3_1Types: Record<Oas3_1NodeType, NodeType> = {
 ...
  NamedDependents: mapOf( { type: 'string' }, listOf({ type: 'string' }))  <<< How do you define the value separately from the key? 

};

dependentRequired : Object<String, Array>
Validation succeeds if, for each name that appears in both the instance and as a name within this keyword’s value, every item in the corresponding array is also the name of a property in the instance.

src: https://www.learnjsonschema.com/2020-12/validation/dependentrequired/

I'm just not clear on how you define that because I don't see any similar types for the current list. If this type definition is correct, I can make the PR for you. Just let me know how to define the type.


repro example

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" },
    "license": { "type": "string" }
  },
  "dependentRequired": {
    "license": [ "age" ]
  }
}
{
  "name": "John",
  "age": 25,
  "license": "XYZ123"
}

@jeremyfiel you're correct. And there is one more issue that I think is related to missing the type there: #1598.