Skip to content

Commit

Permalink
openapi3: Implement YAML Marshaler interface for AdditionalProperties (
Browse files Browse the repository at this point in the history
  • Loading branch information
praneetloke committed Mar 11, 2024
1 parent 05453ef commit 7f46bdf
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/docs/openapi3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ type AdditionalProperties struct {
func (addProps AdditionalProperties) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of AdditionalProperties.

func (addProps AdditionalProperties) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of AdditionalProperties.

func (addProps *AdditionalProperties) UnmarshalJSON(data []byte) error
UnmarshalJSON sets AdditionalProperties to a copy of data.

Expand Down
40 changes: 40 additions & 0 deletions openapi3/additionalProperties_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package openapi3_test

import (
"bytes"
"context"
"os"
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"

"github.com/getkin/kin-openapi/openapi3"
)

func TestMarshalAdditionalProperties(t *testing.T) {
ctx := context.Background()
data, err := os.ReadFile("testdata/test.openapi.additionalproperties.yml")
require.NoError(t, err)

loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
spec, err := loader.LoadFromData(data)
require.NoError(t, err)

err = spec.Validate(ctx)
require.NoError(t, err)

var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
err = enc.Encode(spec)
require.NoError(t, err)

// Load the doc from the serialized yaml.
spec2, err := loader.LoadFromData(buf.Bytes())
require.NoError(t, err)

err = spec2.Validate(ctx)
require.NoError(t, err)
}
14 changes: 14 additions & 0 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,20 @@ type AdditionalProperties struct {
Schema *SchemaRef
}

// MarshalYAML returns the YAML encoding of AdditionalProperties.
func (addProps AdditionalProperties) MarshalYAML() (interface{}, error) {
if x := addProps.Has; x != nil {
if *x {
return true, nil
}
return false, nil
}
if x := addProps.Schema; x != nil {
return x.Value, nil
}
return nil, nil
}

// MarshalJSON returns the JSON encoding of AdditionalProperties.
func (addProps AdditionalProperties) MarshalJSON() ([]byte, error) {
if x := addProps.Has; x != nil {
Expand Down
17 changes: 17 additions & 0 deletions openapi3/testdata/test.openapi.additionalproperties.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
openapi: 3.0.0
info:
title: "OAI Specification in YAML"
version: 0.0.1
paths: {}
components:
schemas:
TestSchema:
type: object
additionalProperties:
type: array
items:
type: string
TestSchema2:
type: object
additionalProperties:
type: string

0 comments on commit 7f46bdf

Please sign in to comment.