Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openapi3: Implement YAML Marshaler interface for AdditionalProperties #922

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading