redocly-cli icon indicating copy to clipboard operation
redocly-cli copied to clipboard

no-invalid-media-type-examples | not working as expected

Open jeremyfiel opened this issue 1 year ago • 11 comments

Describe the bug

i've looked at the OAS schema and it uses either a ref or example object. We are using an example schema in our definition but the external file is not a valid example per the linter. I can't share the full schema for a repo of the issue but my external schema file is matching the expected response body schema, is there any reason why the linter says it should be type object? my external document is a valid uri

$ redocly lint --config ~/repos/.redocly.yaml openapi.json --format stylish

validating specs/openapi.json...
openapi.json:
  101:22  warning  no-invalid-media-type-examples  Example value must conform to the schema: type must be object.

openapi.json

"content": {
	"application/json": {
		"schema": {
			"$ref": "./schemas/response-schema_v01.json"
		},
		"examples": {
			"response": {
				"externalValue": "./examples/success-response_v01.json"
			}
		}
	}
}

Response body schema

{
	"$schema": "http://json-schema.org/draft-04/schema#",
	"title": "ActivityLogs",
	"description": "Activity Logs",
	"type": "object",
	"properties": {
		"ActivityLogs": {
			"type": "array",
			"items": {
				"type": "object",
				"additionalProperties": false,
				"properties": {
					"activityLog": {
						"$ref": "../../../../common/logEntryBaseType_v02.json"
					},
					"links": {
						"type": "array",
						"items": {
							"$ref": "../../../../common/linkType_v01.json"
						}
					}
				}
			}
		}
	},
	"additionalProperties": false
}

Example

{
	"ActivityLogs": [
		{ 
                      "activityLog": { ...

OAS meta schema

"examples": {
        "properties": {
          "example": true,
          "examples": {
            "type": "object",
            "additionalProperties": {
              "$ref": "#/$defs/example-or-reference"
            }
          }
        }
      },
"$defs": {
"example-or-reference": {
        "if": {
          "type": "object",
          "required": [
            "$ref"
          ]
        },
        "then": {
          "$ref": "#/$defs/reference"
        },
        "else": {
          "$ref": "#/$defs/example"
        }
      }
},
"example": {
        "$comment": "https://spec.openapis.org/oas/v3.1.0#example-object",
        "type": "object",
        "properties": {
          "summary": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "value": true,
          "externalValue": {
            "type": "string",
            "format": "uri"
          }
        },
        "not": {
          "required": [
            "value",
            "externalValue"
          ]
        },
        "$ref": "#/$defs/specification-extensions",
        "unevaluatedProperties": false
      }



jeremyfiel avatar Oct 26 '22 19:10 jeremyfiel

Note for our team: I wonder if we have tests with externalValue and are resolving them?

adamaltman avatar Oct 27 '22 13:10 adamaltman

@adamaltman we have tests for the no-example-value-and-externalValue rule, however we are not resolving externalValues at the moment.

tatomyr avatar Oct 31 '22 10:10 tatomyr

@tatomyr does the tool compare examples with the response schema? from your comment, it sounds like this validation is not performed but the error provided is highlighting my example external schema file in VS Code.

image

jeremyfiel avatar Oct 31 '22 15:10 jeremyfiel

@jeremyfiel do you mean the highlighted file is being referenced by externalValue field? We do not resolve such links yet. Could it by any chance be referenced by $ref from another place?

tatomyr avatar Nov 02 '22 10:11 tatomyr

Yes, I can show you on slack if you want to see

jeremyfiel avatar Nov 02 '22 14:11 jeremyfiel

@jeremyfiel sure, feel free to ping me: [email protected]

tatomyr avatar Nov 02 '22 14:11 tatomyr

i've tried to make a solid example of different locations and usage of examples in OAS3.0.x and OAS3.1.x based on discussion with Andrew in our call.

I'm asking for Redocly to support externalValue $ref resolution to validate examples against the request/response/example/header/parameter schema.

I may be able to provide some development effort from our internal tooling team to make this happen. If there is interest, we can arrange further discussion for how you would like the feature added.

redocly-examples-repo.md

jeremyfiel avatar Nov 10 '22 23:11 jeremyfiel

Thanks for summing it up!

tatomyr avatar Nov 11 '22 15:11 tatomyr

I found a workaround to externalValue by using $ref.

it's not super pretty, but it seems to work ok.

Because the examples collection expects example objects, creating the example object schema with summary, value satisfies the defined JSON Schema when the $ref is parsed

## external example file  ./examples/confirmMessage-example_v01.json
{
    "summary": "confirmMessage-example",
    "value": {
        "confirmMessageID": "391708e2-b82a-4e40-8079-29256022a85d",
        "processStatus": "success"
    }
}
## oas definition
"responses": {
	"400": {
		"description": "Bad Request",
		"headers": {
			...
		},
		"content": {
			"application/json": {
				"schema": {
					"$ref": "../../../common/shared/confirm-message-schema_v03.json"
				},
				"examples": {
					"confirmMessage": {
						"$ref": "./examples/confirmMessage-example_v01.json"
					}
				}
			}
		}
	}
                }

jeremyfiel avatar Apr 18 '23 22:04 jeremyfiel

We should be careful about assuming that we can resolve/parse externalValue the same way we do other references, the purpose of it is to allow examples that cannot easily be added in JSON or YAML formats (see https://spec.openapis.org/oas/v3.1.0#exampleObject). It should be expected to be a string though, not an object, I'm not sure if that's currently the case.

The workaround here, using $ref to point to the example in another file, I think is the best approach - @jeremyfiel does Redocly CLI correctly check the example against the schema when you do it this way?

(just noticed we should wish a happy birthday to this issue, I thought it had been open a while! :birthday: )

lornajane avatar Oct 26 '23 12:10 lornajane

I thnk it's reasonable to expect the use of $ref over externalValue. You're right about the intention is to express non-json/yaml strings. Because it accepts a uri-reference I think checking for a filename extension prior to validation would be a reasonable compromise to enable this functionality.

It does check the example but it fails with schema id or key exists for reused schemas. There's another bug filed for this issue for windows users. (Myself)

jeremyfiel avatar Oct 26 '23 13:10 jeremyfiel