metadevpro / openapi3-ts

TS Model & utils for creating and exposing OpenAPI 3.x contracts.
MIT License
493 stars 64 forks source link

Validation error: `examples` must be an array #127

Closed RobinTail closed 11 months ago

RobinTail commented 11 months ago

Using the recent version (4.2.0) I'm getting OpenAPI 3.1 validation error regarding SchemaObject's examples being object instead of array.

image

It's caused by the changes in this PR: https://github.com/metadevpro/openapi3-ts/pull/124 In that PR examples were changed from any[] to ExamplesObject.

The specification refers to this document: https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#page-22 which as well as the validator says that examples must be array.

image

I'm trying to find the source of truth and/or where the mistake is.

@kkolstad || @pjmolina , could you tell me please which information the changes from #124 are based on? Is there any other RFC or revision that changed the examples from array to object? Is that an issue of the validator or of the PR?

kkolstad commented 11 months ago

I based those changes off the OpenAPI Specification 3.0 and 3.1

Screenshot 2023-12-21 at 2 31 57 PM
RobinTail commented 11 months ago

The examples you refer to, are NOT the property of SchemaObject. This is the examples property of ComponentsObject, @kkolstad

image

image

Anyway, thank you very much for responding.

RobinTail commented 11 months ago

@pjmolina , it seems that the changes made by #124 are wrong, because the property examples exists in multiple interfaces. In most cases it's really the ExampleObject, but within the SchemaObject examples still has to be an array. I'm suggesting to revert #124 .

kkolstad commented 11 months ago

@RobinTail I see my mistake now. Thanks for pointing that out. Sorry for the bug 😄

pjmolina commented 11 months ago

Thansk for the analysis @RobinTail

vafanassieff commented 5 months ago

Hey @kkolstad when I read the Open API spec 3.1, i see that ParameterObject has a examples field that can be set to Map[ string, Example Object | Reference Object]

How do i use this type to add examples to my Open Api spec ?

Best

RobinTail commented 5 months ago

Regarding the particular examples in ParameterObject you refer to, @vafanassieff:

What they call Map is a Record in Typescript. So you assign examples with object having keys as string and values as ExampleObject | ReferenceObject

...
examples: {
  example1: {
    value: "something"
  }
}

See the definition of ExampleObject here: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#example-object

It's typed correctly in the library and not related to this issue.