Skip to content

Commit

Permalink
changes for go-swagger #2054 - extension fields in info.contact do no…
Browse files Browse the repository at this point in the history
…t propagate to embedded spec

Signed-off-by: kul-amr <amruta9.kulkarni@gmail.com>
  • Loading branch information
kul-amr committed Jan 13, 2020
1 parent 363c88a commit bc429e5
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 22 deletions.
30 changes: 30 additions & 0 deletions contact_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,41 @@

package spec

import (
"encoding/json"
"github.com/go-openapi/swag"
)

// ContactInfo contact information for the exposed API.
//
// For more information: http://goo.gl/8us55a#contactObject
type ContactInfo struct {
ContactInfoProps
VendorExtensible
}

type ContactInfoProps struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Email string `json:"email,omitempty"`
}

func (c *ContactInfo) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &c.ContactInfoProps); err != nil {
return err
}
return json.Unmarshal(data, &c.VendorExtensible)
}

func (c ContactInfo) MarshalJSON() ([]byte, error) {
b1, err := json.Marshal(c.ContactInfoProps)
if err != nil {
return nil, err
}
b2, err := json.Marshal(c.VendorExtensible)
if err != nil {
return nil, err
}
return swag.ConcatJSON(b1, b2), nil
}

35 changes: 22 additions & 13 deletions contact_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,33 @@
package spec

import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
)

const contactInfoJSON = `{"name":"wordnik api team","url":"http://developer.wordnik.com","email":"some@mailayada.dkdkd"}`
const contactInfoYAML = `name: wordnik api team
url: http://developer.wordnik.com
email: some@mailayada.dkdkd
`
const contactInfoJSON = `{
"name": "wordnik api team",
"url": "http://developer.wordnik.com",
"email": "some@mailayada.dkdkd",
"x-teams": "test team"
}`

var contactInfo = ContactInfo{
Name: "wordnik api team",
URL: "http://developer.wordnik.com",
var contactInfo = ContactInfo{ContactInfoProps:ContactInfoProps{
Name: "wordnik api team",
URL: "http://developer.wordnik.com",
Email: "some@mailayada.dkdkd",
}
},VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{"x-teams": "test team"}},}

func TestIntegrationContactInfo(t *testing.T) {
assertSerializeJSON(t, contactInfo, contactInfoJSON)
assertSerializeYAML(t, contactInfo, contactInfoYAML)
assertParsesJSON(t, contactInfoJSON, contactInfo)
assertParsesYAML(t, contactInfoYAML, contactInfo)
b, err := json.MarshalIndent(contactInfo, "", "\t")
if assert.NoError(t, err) {
assert.Equal(t, contactInfoJSON, string(b))
}

actual := ContactInfo{}
err = json.Unmarshal([]byte(contactInfoJSON), &actual)
if assert.NoError(t, err) {
assert.EqualValues(t, contactInfo, actual)
}
}
5 changes: 3 additions & 2 deletions info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ var info = Info{
Description: "A sample API that uses a petstore as an example to demonstrate features in " +
"the swagger-2.0 specification",
TermsOfService: "http://helloreverb.com/terms/",
Contact: &ContactInfo{Name: "wordnik api team", URL: "http://developer.wordnik.com"},
License: &License{
Contact: &ContactInfo{ContactInfoProps:ContactInfoProps{Name: "wordnik api team", URL: "http://developer.wordnik.com"}},
License: &License{LicenseProps:LicenseProps{
Name: "Creative Commons 4.0 International",
URL: "http://creativecommons.org/licenses/by/4.0/",
},
},
},
VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{"x-framework": "go-swagger"}},
}
Expand Down
30 changes: 30 additions & 0 deletions license.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,40 @@

package spec

import (
"encoding/json"
"github.com/go-openapi/swag"
)

// License information for the exposed API.
//
// For more information: http://goo.gl/8us55a#licenseObject
type License struct {
LicenseProps
VendorExtensible
}

type LicenseProps struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
}


func (l *License) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &l.LicenseProps); err != nil {
return err
}
return json.Unmarshal(data, &l.VendorExtensible)
}

func (l License) MarshalJSON() ([]byte, error) {
b1, err := json.Marshal(l.LicenseProps)
if err != nil {
return nil, err
}
b2, err := json.Marshal(l.VendorExtensible)
if err != nil {
return nil, err
}
return swag.ConcatJSON(b1, b2), nil
}
33 changes: 26 additions & 7 deletions license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,34 @@

package spec

import "testing"
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
)

var license = License{
LicenseProps:LicenseProps{Name: "the name", URL: "the url"},
VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{"x-license": "custom term"}},}

const licenseJSON = `{
"name": "the name",
"url": "the url",
"x-license": "custom term"
}`

func TestIntegrationLicense(t *testing.T) {
license := License{Name: "the name", URL: "the url"}
const licenseJSON = `{"name":"the name","url":"the url"}`

const licenseYAML = "name: the name\nurl: the url\n"

assertSerializeJSON(t, license, licenseJSON)
assertSerializeYAML(t, license, licenseYAML)
assertParsesJSON(t, licenseJSON, license)
assertParsesYAML(t, licenseYAML, license)
b, err := json.MarshalIndent(license, "", "\t")
if assert.NoError(t, err) {
assert.Equal(t, licenseJSON, string(b))
}

actual := License{}
err = json.Unmarshal([]byte(licenseJSON), &actual)
if assert.NoError(t, err) {
assert.EqualValues(t, license, actual)
}
}

0 comments on commit bc429e5

Please sign in to comment.