Skip to content

Commit

Permalink
Merge branch 'go-openapi:master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
jiuker authored Aug 9, 2023
2 parents 03f57cf + c9f1d67 commit 3fd9e2a
Show file tree
Hide file tree
Showing 21 changed files with 183 additions and 221 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: 1.18

- name: Setup gotestsum
uses: autero1/action-gotestsum@v1.0.0
Expand All @@ -41,6 +41,7 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: golangci/golangci-lint-action@v2
- uses: golangci/golangci-lint-action@v3
with:
version: v1.53
args: --timeout=5m
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ linters:
- tparallel
- paralleltest
- cyclop # because we have gocyclo already
- depguard # we do not add a config for this
# TODO: review the linters below. We disabled them to make the CI pass first.
- nonamedreturns
- exhaustruct
- nosnakecase
- nolintlint
- ireturn
- varnamelen
- forcetypeassert
Expand Down
32 changes: 0 additions & 32 deletions appveyor.yml

This file was deleted.

3 changes: 2 additions & 1 deletion default_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (d *defaultValidator) validateDefaultInResponse(resp *spec.Response, respon

responseName, responseCodeAsStr := responseHelp.responseMsgVariants(responseType, responseCode)

// nolint: dupl
//nolint: dupl
if response.Headers != nil { // Safeguard
for nm, h := range response.Headers {
// reset explored schemas to get depth-first recursive-proof exploration
Expand Down Expand Up @@ -262,6 +262,7 @@ func (d *defaultValidator) validateDefaultValueSchemaAgainstSchema(path, in stri
}

// TODO: Temporary duplicated code. Need to refactor with examples

// nolint: dupl
func (d *defaultValidator) validateDefaultValueItemsAgainstSchema(path, in string, root interface{}, items *spec.Items) *Result {
res := new(Result)
Expand Down
70 changes: 36 additions & 34 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ as well as tools to validate data against their schema.
This package follows Swagger 2.0. specification (aka OpenAPI 2.0). Reference
can be found here: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md.
Validating a specification
# Validating a specification
Validates a spec document (from JSON or YAML) against the JSON schema for swagger,
then checks a number of extra rules that can't be expressed in JSON schema.
Expand All @@ -30,34 +30,36 @@ Entry points:
- SpecValidator.Validate()
Reported as errors:
[x] definition can't declare a property that's already defined by one of its ancestors
[x] definition's ancestor can't be a descendant of the same model
[x] path uniqueness: each api path should be non-verbatim (account for path param names) unique per method
[x] each security reference should contain only unique scopes
[x] each security scope in a security definition should be unique
[x] parameters in path must be unique
[x] each path parameter must correspond to a parameter placeholder and vice versa
[x] each referenceable definition must have references
[x] each definition property listed in the required array must be defined in the properties of the model
[x] each parameter should have a unique `name` and `type` combination
[x] each operation should have only 1 parameter of type body
[x] each reference must point to a valid object
[x] every default value that is specified must validate against the schema for that property
[x] items property is required for all schemas/definitions of type `array`
[x] path parameters must be declared a required
[x] headers must not contain $ref
[x] schema and property examples provided must validate against their respective object's schema
[x] examples provided must validate their schema
[x] definition can't declare a property that's already defined by one of its ancestors
[x] definition's ancestor can't be a descendant of the same model
[x] path uniqueness: each api path should be non-verbatim (account for path param names) unique per method
[x] each security reference should contain only unique scopes
[x] each security scope in a security definition should be unique
[x] parameters in path must be unique
[x] each path parameter must correspond to a parameter placeholder and vice versa
[x] each referenceable definition must have references
[x] each definition property listed in the required array must be defined in the properties of the model
[x] each parameter should have a unique `name` and `type` combination
[x] each operation should have only 1 parameter of type body
[x] each reference must point to a valid object
[x] every default value that is specified must validate against the schema for that property
[x] items property is required for all schemas/definitions of type `array`
[x] path parameters must be declared a required
[x] headers must not contain $ref
[x] schema and property examples provided must validate against their respective object's schema
[x] examples provided must validate their schema
Reported as warnings:
[x] path parameters should not contain any of [{,},\w]
[x] empty path
[x] unused definitions
[x] unsupported validation of examples on non-JSON media types
[x] examples in response without schema
[x] readOnly properties should not be required
Validating a schema
[x] path parameters should not contain any of [{,},\w]
[x] empty path
[x] unused definitions
[x] unsupported validation of examples on non-JSON media types
[x] examples in response without schema
[x] readOnly properties should not be required
# Validating a schema
The schema validation toolkit validates data against JSON-schema-draft 04 schema.
Expand All @@ -70,16 +72,16 @@ Entry points:
- AgainstSchema()
- ...
Known limitations
# Known limitations
With the current version of this package, the following aspects of swagger are not yet supported:
[ ] errors and warnings are not reported with key/line number in spec
[ ] default values and examples on responses only support application/json producer type
[ ] invalid numeric constraints (such as Minimum, etc..) are not checked except for default and example values
[ ] rules for collectionFormat are not implemented
[ ] no validation rule for polymorphism support (discriminator) [not done here]
[ ] valid js ECMA regexp not supported by Go regexp engine are considered invalid
[ ] arbitrary large numbers are not supported: max is math.MaxFloat64
[ ] errors and warnings are not reported with key/line number in spec
[ ] default values and examples on responses only support application/json producer type
[ ] invalid numeric constraints (such as Minimum, etc..) are not checked except for default and example values
[ ] rules for collectionFormat are not implemented
[ ] no validation rule for polymorphism support (discriminator) [not done here]
[ ] valid js ECMA regexp not supported by Go regexp engine are considered invalid
[ ] arbitrary large numbers are not supported: max is math.MaxFloat64
*/
package validate
8 changes: 7 additions & 1 deletion doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ExampleSpec() {
// Output: This spec is valid
}

func ExampleSpec_url() {
func ExampleSpec_second() {
// Example with high level spec validation call, without showing warnings

// Example with online spec URL:
Expand All @@ -67,6 +67,9 @@ func ExampleSpec_url() {
} else {
fmt.Println("Could not load this spec")
}

// Output:
// This spec is valid
}

func ExampleSpecValidator_Validate() {
Expand Down Expand Up @@ -116,6 +119,9 @@ func ExampleSpecValidator_Validate_url() {
fmt.Println("This spec has some validation warnings")
}
}

// Output:
// This spec is valid
}

func ExampleAgainstSchema() {
Expand Down
1 change: 0 additions & 1 deletion example_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func (ex *exampleValidator) isVisited(path string) bool {
// - schemas
// - individual property
// - responses
//
func (ex *exampleValidator) Validate() (errs *Result) {
errs = new(Result)
if ex == nil || ex.SpecValidator == nil {
Expand Down
5 changes: 1 addition & 4 deletions fixtures/validation/fixture-1171.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ paths:
responses:
'200':
schema:
# $ref may not have sibling
properties:
name: sibling
$ref: '#/definitions/Zones'
$ref: '#/definitions/Zones'

'/servers/{server_id}/zones/{zone_id}':
get:
Expand Down
32 changes: 23 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
module github.com/go-openapi/validate

go 1.14
go 1.18

require (
github.com/go-openapi/analysis v0.21.2
github.com/go-openapi/errors v0.19.9
github.com/go-openapi/jsonpointer v0.19.5
github.com/go-openapi/loads v0.21.1
github.com/go-openapi/spec v0.20.4
github.com/go-openapi/strfmt v0.21.1
github.com/go-openapi/swag v0.21.1
github.com/stretchr/testify v1.7.0
github.com/go-openapi/analysis v0.21.4
github.com/go-openapi/errors v0.20.4
github.com/go-openapi/jsonpointer v0.20.0
github.com/go-openapi/loads v0.21.2
github.com/go-openapi/spec v0.20.9
github.com/go-openapi/strfmt v0.21.7
github.com/go-openapi/swag v0.22.4
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
go.mongodb.org/mongo-driver v1.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 3fd9e2a

Please sign in to comment.