Skip to content

Commit

Permalink
Merge pull request #2 from Percona-Lab/PMM-5645-0.19.8-percona
Browse files Browse the repository at this point in the history
PMM-5645 0.19.8 percona
  • Loading branch information
askomorokhov authored Jun 30, 2020
2 parents 7447963 + a813d3e commit 00215f1
Show file tree
Hide file tree
Showing 18 changed files with 315 additions and 89 deletions.
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ linters:
- lll
- gochecknoinits
- gochecknoglobals
- funlen
- godox
- gocognit
- whitespace
- wsl
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
after_success:
- bash <(curl -s https://codecov.io/bash)
go:
- 1.11.x
- 1.12.x
- 1.13.x
- 1.14.x
install:
- GO111MODULE=off go get -u gotest.tools/gotestsum
env:
- GO111MODULE=on
language: go
notifications:
slack:
Expand Down
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
}
32 changes: 21 additions & 11 deletions contact_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,34 @@
package spec

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

const contactInfoJSON = `{"name":"wordnik api team","url":"http://developer.wordnik.com","email":"[email protected]"}`
const contactInfoYAML = `name: wordnik api team
url: http://developer.wordnik.com
email: [email protected]
`
const contactInfoJSON = `{
"name": "wordnik api team",
"url": "http://developer.wordnik.com",
"email": "[email protected]",
"x-teams": "test team"
}`

var contactInfo = ContactInfo{
var contactInfo = ContactInfo{ContactInfoProps: ContactInfoProps{
Name: "wordnik api team",
URL: "http://developer.wordnik.com",
Email: "[email protected]",
}
}, 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)
}
}
17 changes: 12 additions & 5 deletions expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ func ExpandSpec(spec *Swagger, options *ExpandOptions) error {
return nil
}

const rootBase = "root"
// baseForRoot loads in the cache the root document and produces a fake "root" base path entry
// for further $ref resolution
func baseForRoot(root interface{}, cache ResolutionCache) string {
// cache the root document to resolve $ref's
const rootBase = "root"
if root != nil {
base, _ := absPath(rootBase)
normalizedBase := normalizeAbsPath(base)
Expand Down Expand Up @@ -339,7 +339,13 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, ba

basePath = resolver.updateBasePath(transitiveResolver, normalizedBasePath)

return expandSchema(*t, parentRefs, transitiveResolver, basePath)
t, err := expandSchema(*t, parentRefs, transitiveResolver, basePath)
if t != nil {
for k, v := range target.VendorExtensible.Extensions {
t.VendorExtensible.AddExtension(k, v)
}
}
return t, err
}
}

Expand Down Expand Up @@ -452,11 +458,12 @@ func expandPathItem(pathItem *PathItem, resolver *schemaLoader, basePath string)
return err
}
if pathItem.Ref.String() != "" {
var err error
resolver, err = resolver.transitiveResolver(basePath, pathItem.Ref)
if resolver.shouldStopOnError(err) {
transitiveResolver, err := resolver.transitiveResolver(basePath, pathItem.Ref)
if transitiveResolver.shouldStopOnError(err) {
return err
}
basePath = transitiveResolver.updateBasePath(resolver, basePath)
resolver = transitiveResolver
}
pathItem.Ref = Ref{}

Expand Down
21 changes: 21 additions & 0 deletions expander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,25 @@ func TestExpandResponseAndParamWithRoot(t *testing.T) {
assert.Nil(t, m)
}

func TestExpandResponseWithRoot_CircularRefs(t *testing.T) {
rootDoc := new(Swagger)
b, err := ioutil.ReadFile("fixtures/more_circulars/resp.json")
if assert.NoError(t, err) && assert.NoError(t, json.Unmarshal(b, rootDoc)) {
path := rootDoc.Paths.Paths["/api/v1/getx"]
resp := path.Post.Responses.StatusCodeResponses[200]

resCache = initResolutionCache()

// during first response expand, refs are getting expanded,
// so the following expands cannot properly resolve them w/o the document.
// this happens in validator.Validate() when different validators try to expand the same mutable response.
err := ExpandResponseWithRoot(&resp, rootDoc, resCache)
assert.NoError(t, err)
err = ExpandResponseWithRoot(&resp, rootDoc, resCache)
assert.NoError(t, err)
}
}

func TestResolveParam(t *testing.T) {
specDoc, err := jsonDoc("fixtures/expansion/all-the-things.json")
if !assert.NoError(t, err) {
Expand Down Expand Up @@ -439,6 +458,7 @@ func TestParameterExpansion(t *testing.T) {

param = spec.Paths.Paths["/cars/{id}"].Parameters[0]
expected = spec.Parameters["id"]
expected.VendorExtensible = param.VendorExtensible

err = expandParameterOrResponse(&param, resolver, basePath)
assert.NoError(t, err)
Expand All @@ -465,6 +485,7 @@ func TestExportedParameterExpansion(t *testing.T) {

param = spec.Paths.Paths["/cars/{id}"].Parameters[0]
expected = spec.Parameters["id"]
expected.VendorExtensible = param.VendorExtensible

err = ExpandParameter(&param, basePath)
assert.NoError(t, err)
Expand Down
9 changes: 9 additions & 0 deletions fixtures/bugs/2113/base.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
swagger: "2.0"
info:
version: "1"
title: "nested $ref fixture"
paths:
/dummy:
$ref: ./schemas/api/api.yaml#/paths/~1dummy
/example:
$ref: ./schemas/api/api.yaml#/paths/~1example
19 changes: 19 additions & 0 deletions fixtures/bugs/2113/schemas/api/api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
swagger: "2.0"
info:
version: "1"
title: "sub api"
paths:
/dummy:
get:
responses:
200:
description: OK
schema:
$ref: ../dummy/dummy.yaml
/example:
get:
responses:
200:
description: OK
schema:
$ref: ../example/example.yaml
5 changes: 5 additions & 0 deletions fixtures/bugs/2113/schemas/dummy/dummy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
required:
- dummyPayload
properties:
dummyPayload:
type: string
6 changes: 6 additions & 0 deletions fixtures/bugs/2113/schemas/example/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$schema: http://json-schema.org/draft-07/schema#
required:
- payload
properties:
payload:
type: string
7 changes: 5 additions & 2 deletions fixtures/expansion/params.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
"paths": {
"/cars/{id}": {
"parameters": [
{ "$ref": "#/parameters/id"}
{
"$ref": "#/parameters/id",
"x-order": 1
}
]
}
}
}
}
51 changes: 51 additions & 0 deletions fixtures/more_circulars/resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"swagger": "2.0",
"info": {
"description": "TestSwagger",
"version": "1.0",
"title": "Test"
},
"host": "127.0.0.1:8443",
"basePath": "/",
"schemes": [
"https"
],
"paths": {
"/api/v1/getx": {
"post": {
"operationId": "getx",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "Operation successful",
"schema": {
"$ref": "#/definitions/MyObj"
}
}
},
"security": []
}
}
},
"definitions": {
"MyObj": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"child-objects": {
"type": "array",
"items": {
"$ref": "#/definitions/MyObj"
}
}
}
}
}
}
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
}
Loading

0 comments on commit 00215f1

Please sign in to comment.