Skip to content

Commit c333b34

Browse files
reuvenharrisonJillefenollpalexbakkerjlsherrill
authored
openapi3: reference originating locations in YAML specs - step 1 (#1007)
* add origin - step 1 * delete origin from content * point to Origin * make include origin configurable * generic unmarshalStringMap * add origin to more components * revert comments * use const originKey * comment to Scopes * test more specs * Fix Discriminator * update docs * test on a decent set of dedicated specs * remove trainling spaces * add comments * remove trailing whitespace * update docs * dedicated tests * update docs * remove whitespace * rm empty line * rm last newline * add tests * rm unused test files * update deps * fix paths test * test components/security * fix LF * include origin in unmarshal maps * move component unmarshallers to respective files * fix test (json-schema 301) * Add github.com/pb33f/libopenapi (#1004) * Add github.com/pb33f/libopenapi it looks like a reasonable alternative * Update README.md --------- Co-authored-by: Pierre Fenoll <[email protected]> * Introduce an option to override the regex implementation (#1006) * make form required field order deterministic (#1008) * openapi2: fix un/marshalling discriminator field (#1011) * fix: issue unmarshalling when discriminator field is set in openapi2.0 * revert original approach * update with different approach * Revert "update with different approach" This reverts commit 2db2b39. * v2 schema with discriminator field set as string * update ref link and comment * run docs.sh * README: add Fuego to dependents (#1017) * openapi3: skip a test in CI to avoid 403s from some remote server (#1019) Signed-off-by: Pierre Fenoll <[email protected]> * revert fix test (json-schema 301) * openapi3: introduce StringMap type to enable unmarshalling of maps with Origin (#1018) * add origin to more components * Fix Discriminator * update docs * update doccs * merge with stringmap * remove unused Scopes --------- Signed-off-by: Pierre Fenoll <[email protected]> Co-authored-by: Jille Timmermans <[email protected]> Co-authored-by: Pierre Fenoll <[email protected]> Co-authored-by: Alexander Bakker <[email protected]> Co-authored-by: Justin Sherrill <[email protected]> Co-authored-by: Jay Shah <[email protected]> Co-authored-by: Ewen Quimerc'h <[email protected]>
1 parent 56505dc commit c333b34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+816
-14
lines changed

.github/docs/openapi3.txt

Lines changed: 88 additions & 1 deletion
Large diffs are not rendered by default.

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
module github.com/getkin/kin-openapi
22

3-
go 1.20
3+
go 1.22.5
4+
5+
replace gopkg.in/yaml.v3 => github.com/oasdiff/yaml3 v0.0.0-20240920135353-c185dc6ea7c6
6+
7+
replace github.com/invopop/yaml => github.com/oasdiff/yaml v0.0.0-20240920191703-3e5a9fb5bdf3
48

59
require (
610
github.com/go-openapi/jsonpointer v0.21.0

go.sum

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kO
55
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
66
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
77
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
8+
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
89
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
910
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
10-
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=
11-
github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81KkeA=
1211
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
1312
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
1413
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
14+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
1515
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
16+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1617
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
1718
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
1819
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
1920
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
21+
github.com/oasdiff/yaml v0.0.0-20240920191703-3e5a9fb5bdf3 h1:nqCxALSUgWobWkFGIrhLRzR/bpImQdGj+3JS4/scTJo=
22+
github.com/oasdiff/yaml v0.0.0-20240920191703-3e5a9fb5bdf3/go.mod h1:AOyUNV9ElKz7EEZeBm/48U54UtjtgCMT9fFbZEsClQc=
23+
github.com/oasdiff/yaml3 v0.0.0-20240920135353-c185dc6ea7c6 h1:+ZsuDTdapTJxfMQk7SOJiNMg0v36pui01L7FEO615r8=
24+
github.com/oasdiff/yaml3 v0.0.0-20240920135353-c185dc6ea7c6/go.mod h1:lqlOfJRrYpgeWHQj+ky2tf7UJ3PzgHTHRQEpc90nbp0=
2025
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
2126
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
2227
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -26,7 +31,6 @@ github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99
2631
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
2732
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
2833
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
29-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
34+
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
3035
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
31-
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
32-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
36+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

maps.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@ func (${name} ${type}) UnmarshalJSON(data []byte) (err error) {
209209
continue
210210
}
211211
212+
if k == originKey {
213+
var data []byte
214+
if data, err = json.Marshal(v); err != nil {
215+
return
216+
}
217+
if err = json.Unmarshal(data, &x.Origin); err != nil {
218+
return
219+
}
220+
continue
221+
}
222+
212223
var data []byte
213224
if data, err = json.Marshal(v); err != nil {
214225
return

openapi3/callback.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#callback-object
1010
type Callback struct {
1111
Extensions map[string]any `json:"-" yaml:"-"`
12+
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
1213

1314
m map[string]*PathItem
1415
}
@@ -52,3 +53,9 @@ func (callback *Callback) Validate(ctx context.Context, opts ...ValidationOption
5253

5354
return validateExtensions(ctx, callback.Extensions)
5455
}
56+
57+
// UnmarshalJSON sets Callbacks to a copy of data.
58+
func (callbacks *Callbacks) UnmarshalJSON(data []byte) (err error) {
59+
*callbacks, _, err = unmarshalStringMapP[CallbackRef](data)
60+
return
61+
}

openapi3/components.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type (
2525
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#components-object
2626
type Components struct {
2727
Extensions map[string]any `json:"-" yaml:"-"`
28+
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
2829

2930
Schemas Schemas `json:"schemas,omitempty" yaml:"schemas,omitempty"`
3031
Parameters ParametersMap `json:"parameters,omitempty" yaml:"parameters,omitempty"`
@@ -94,6 +95,7 @@ func (components *Components) UnmarshalJSON(data []byte) error {
9495
return unmarshalError(err)
9596
}
9697
_ = json.Unmarshal(data, &x.Extensions)
98+
delete(x.Extensions, originKey)
9799
delete(x.Extensions, "schemas")
98100
delete(x.Extensions, "parameters")
99101
delete(x.Extensions, "headers")

openapi3/contact.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#contact-object
1010
type Contact struct {
1111
Extensions map[string]any `json:"-" yaml:"-"`
12+
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
1213

1314
Name string `json:"name,omitempty" yaml:"name,omitempty"`
1415
URL string `json:"url,omitempty" yaml:"url,omitempty"`
@@ -50,6 +51,8 @@ func (contact *Contact) UnmarshalJSON(data []byte) error {
5051
return unmarshalError(err)
5152
}
5253
_ = json.Unmarshal(data, &x.Extensions)
54+
55+
delete(x.Extensions, originKey)
5356
delete(x.Extensions, "name")
5457
delete(x.Extensions, "url")
5558
delete(x.Extensions, "email")

openapi3/content.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,9 @@ func (content Content) Validate(ctx context.Context, opts ...ValidationOption) e
122122
}
123123
return nil
124124
}
125+
126+
// UnmarshalJSON sets Content to a copy of data.
127+
func (content *Content) UnmarshalJSON(data []byte) (err error) {
128+
*content, _, err = unmarshalStringMapP[MediaType](data)
129+
return
130+
}

openapi3/discriminator.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#discriminator-object
1010
type Discriminator struct {
1111
Extensions map[string]any `json:"-" yaml:"-"`
12+
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
1213

1314
PropertyName string `json:"propertyName" yaml:"propertyName"` // required
1415
Mapping StringMap `json:"mapping,omitempty" yaml:"mapping,omitempty"`
@@ -44,6 +45,8 @@ func (discriminator *Discriminator) UnmarshalJSON(data []byte) error {
4445
return unmarshalError(err)
4546
}
4647
_ = json.Unmarshal(data, &x.Extensions)
48+
49+
delete(x.Extensions, originKey)
4750
delete(x.Extensions, "propertyName")
4851
delete(x.Extensions, "mapping")
4952
if len(x.Extensions) == 0 {

openapi3/encoding.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#encoding-object
1212
type Encoding struct {
1313
Extensions map[string]any `json:"-" yaml:"-"`
14+
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
1415

1516
ContentType string `json:"contentType,omitempty" yaml:"contentType,omitempty"`
1617
Headers Headers `json:"headers,omitempty" yaml:"headers,omitempty"`
@@ -80,6 +81,8 @@ func (encoding *Encoding) UnmarshalJSON(data []byte) error {
8081
return unmarshalError(err)
8182
}
8283
_ = json.Unmarshal(data, &x.Extensions)
84+
85+
delete(x.Extensions, originKey)
8386
delete(x.Extensions, "contentType")
8487
delete(x.Extensions, "headers")
8588
delete(x.Extensions, "style")

openapi3/example.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,9 @@ func (example *Example) Validate(ctx context.Context, opts ...ValidationOption)
8383

8484
return validateExtensions(ctx, example.Extensions)
8585
}
86+
87+
// UnmarshalJSON sets Examples to a copy of data.
88+
func (examples *Examples) UnmarshalJSON(data []byte) (err error) {
89+
*examples, _, err = unmarshalStringMapP[ExampleRef](data)
90+
return
91+
}

openapi3/external_docs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#external-documentation-object
1313
type ExternalDocs struct {
1414
Extensions map[string]any `json:"-" yaml:"-"`
15+
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
1516

1617
Description string `json:"description,omitempty" yaml:"description,omitempty"`
1718
URL string `json:"url,omitempty" yaml:"url,omitempty"`
@@ -49,6 +50,7 @@ func (e *ExternalDocs) UnmarshalJSON(data []byte) error {
4950
return unmarshalError(err)
5051
}
5152
_ = json.Unmarshal(data, &x.Extensions)
53+
delete(x.Extensions, originKey)
5254
delete(x.Extensions, "description")
5355
delete(x.Extensions, "url")
5456
if len(x.Extensions) == 0 {

openapi3/header.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,9 @@ func (header *Header) Validate(ctx context.Context, opts ...ValidationOption) er
9494
}
9595
return nil
9696
}
97+
98+
// UnmarshalJSON sets Headers to a copy of data.
99+
func (headers *Headers) UnmarshalJSON(data []byte) (err error) {
100+
*headers, _, err = unmarshalStringMapP[HeaderRef](data)
101+
return
102+
}

openapi3/info.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#info-object
1111
type Info struct {
1212
Extensions map[string]any `json:"-" yaml:"-"`
13+
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
1314

1415
Title string `json:"title" yaml:"title"` // Required
1516
Description string `json:"description,omitempty" yaml:"description,omitempty"`
@@ -62,6 +63,7 @@ func (info *Info) UnmarshalJSON(data []byte) error {
6263
return unmarshalError(err)
6364
}
6465
_ = json.Unmarshal(data, &x.Extensions)
66+
delete(x.Extensions, originKey)
6567
delete(x.Extensions, "title")
6668
delete(x.Extensions, "description")
6769
delete(x.Extensions, "termsOfService")

openapi3/license.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#license-object
1111
type License struct {
1212
Extensions map[string]any `json:"-" yaml:"-"`
13+
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
1314

1415
Name string `json:"name" yaml:"name"` // Required
1516
URL string `json:"url,omitempty" yaml:"url,omitempty"`
@@ -45,6 +46,7 @@ func (license *License) UnmarshalJSON(data []byte) error {
4546
return unmarshalError(err)
4647
}
4748
_ = json.Unmarshal(data, &x.Extensions)
49+
delete(x.Extensions, originKey)
4850
delete(x.Extensions, "name")
4951
delete(x.Extensions, "url")
5052
if len(x.Extensions) == 0 {

openapi3/link.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#link-object
1212
type Link struct {
1313
Extensions map[string]any `json:"-" yaml:"-"`
14+
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
1415

1516
OperationRef string `json:"operationRef,omitempty" yaml:"operationRef,omitempty"`
1617
OperationID string `json:"operationId,omitempty" yaml:"operationId,omitempty"`
@@ -66,6 +67,8 @@ func (link *Link) UnmarshalJSON(data []byte) error {
6667
return unmarshalError(err)
6768
}
6869
_ = json.Unmarshal(data, &x.Extensions)
70+
71+
delete(x.Extensions, originKey)
6972
delete(x.Extensions, "operationRef")
7073
delete(x.Extensions, "operationId")
7174
delete(x.Extensions, "description")
@@ -92,3 +95,9 @@ func (link *Link) Validate(ctx context.Context, opts ...ValidationOption) error
9295

9396
return validateExtensions(ctx, link.Extensions)
9497
}
98+
99+
// UnmarshalJSON sets Links to a copy of data.
100+
func (links *Links) UnmarshalJSON(data []byte) (err error) {
101+
*links, _, err = unmarshalStringMapP[LinkRef](data)
102+
return
103+
}

openapi3/loader.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ type Loader struct {
2828
// IsExternalRefsAllowed enables visiting other files
2929
IsExternalRefsAllowed bool
3030

31+
// IncludeOrigin specifies whether to include the origin of the OpenAPI elements
32+
IncludeOrigin bool
33+
3134
// ReadFromURIFunc allows overriding the any file/URL reading func
3235
ReadFromURIFunc ReadFromURIFunc
3336

@@ -103,7 +106,7 @@ func (loader *Loader) loadSingleElementFromURI(ref string, rootPath *url.URL, el
103106
if err != nil {
104107
return nil, err
105108
}
106-
if err := unmarshal(data, element); err != nil {
109+
if err := unmarshal(data, element, loader.IncludeOrigin); err != nil {
107110
return nil, err
108111
}
109112

@@ -139,7 +142,7 @@ func (loader *Loader) LoadFromIoReader(reader io.Reader) (*T, error) {
139142
func (loader *Loader) LoadFromData(data []byte) (*T, error) {
140143
loader.resetVisitedPathItemRefs()
141144
doc := &T{}
142-
if err := unmarshal(data, doc); err != nil {
145+
if err := unmarshal(data, doc, loader.IncludeOrigin); err != nil {
143146
return nil, err
144147
}
145148
if err := loader.ResolveRefsIn(doc, nil); err != nil {
@@ -168,7 +171,7 @@ func (loader *Loader) loadFromDataWithPathInternal(data []byte, location *url.UR
168171
doc := &T{}
169172
loader.visitedDocuments[uri] = doc
170173

171-
if err := unmarshal(data, doc); err != nil {
174+
if err := unmarshal(data, doc, loader.IncludeOrigin); err != nil {
172175
return nil, err
173176
}
174177

@@ -422,7 +425,7 @@ func (loader *Loader) resolveComponent(doc *T, ref string, path *url.URL, resolv
422425
if err2 != nil {
423426
return nil, nil, err
424427
}
425-
if err2 = unmarshal(data, &cursor); err2 != nil {
428+
if err2 = unmarshal(data, &cursor, loader.IncludeOrigin); err2 != nil {
426429
return nil, nil, err
427430
}
428431
if cursor, err2 = drill(cursor); err2 != nil || cursor == nil {

openapi3/maplike.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ func (responses *Responses) UnmarshalJSON(data []byte) (err error) {
125125
continue
126126
}
127127

128+
if k == originKey {
129+
var data []byte
130+
if data, err = json.Marshal(v); err != nil {
131+
return
132+
}
133+
if err = json.Unmarshal(data, &x.Origin); err != nil {
134+
return
135+
}
136+
continue
137+
}
138+
128139
var data []byte
129140
if data, err = json.Marshal(v); err != nil {
130141
return
@@ -256,6 +267,17 @@ func (callback *Callback) UnmarshalJSON(data []byte) (err error) {
256267
continue
257268
}
258269

270+
if k == originKey {
271+
var data []byte
272+
if data, err = json.Marshal(v); err != nil {
273+
return
274+
}
275+
if err = json.Unmarshal(data, &x.Origin); err != nil {
276+
return
277+
}
278+
continue
279+
}
280+
259281
var data []byte
260282
if data, err = json.Marshal(v); err != nil {
261283
return
@@ -387,6 +409,17 @@ func (paths *Paths) UnmarshalJSON(data []byte) (err error) {
387409
continue
388410
}
389411

412+
if k == originKey {
413+
var data []byte
414+
if data, err = json.Marshal(v); err != nil {
415+
return
416+
}
417+
if err = json.Unmarshal(data, &x.Origin); err != nil {
418+
return
419+
}
420+
continue
421+
}
422+
390423
var data []byte
391424
if data, err = json.Marshal(v); err != nil {
392425
return

openapi3/marsh.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func unmarshalError(jsonUnmarshalErr error) error {
1616
return jsonUnmarshalErr
1717
}
1818

19-
func unmarshal(data []byte, v any) error {
19+
func unmarshal(data []byte, v any, includeOrigin bool) error {
2020
var jsonErr, yamlErr error
2121

2222
// See https://github.com/getkin/kin-openapi/issues/680
@@ -25,7 +25,7 @@ func unmarshal(data []byte, v any) error {
2525
}
2626

2727
// UnmarshalStrict(data, v) TODO: investigate how ymlv3 handles duplicate map keys
28-
if yamlErr = yaml.Unmarshal(data, v); yamlErr == nil {
28+
if yamlErr = yaml.UnmarshalWithOrigin(data, v, includeOrigin); yamlErr == nil {
2929
return nil
3030
}
3131

openapi3/media_type.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#media-type-object
1515
type MediaType struct {
1616
Extensions map[string]any `json:"-" yaml:"-"`
17+
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
1718

1819
Schema *SchemaRef `json:"schema,omitempty" yaml:"schema,omitempty"`
1920
Example any `json:"example,omitempty" yaml:"example,omitempty"`
@@ -101,6 +102,7 @@ func (mediaType *MediaType) UnmarshalJSON(data []byte) error {
101102
return unmarshalError(err)
102103
}
103104
_ = json.Unmarshal(data, &x.Extensions)
105+
delete(x.Extensions, originKey)
104106
delete(x.Extensions, "schema")
105107
delete(x.Extensions, "example")
106108
delete(x.Extensions, "examples")

0 commit comments

Comments
 (0)