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

WIP on #230 coming from #318 #412

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
88 changes: 88 additions & 0 deletions .github/workflows/go-santhoshtekuri.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: go-santhoshtekuri
on:
pull_request:
push:

jobs:
build-and-test:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GO111MODULE: 'on'
CGO_ENABLED: '0'
strategy:
fail-fast: true
matrix:
go: ['1.14', '1.x']
# Locked at https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on
os:
- ubuntu-20.04
- windows-2019
- macos-10.15
runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash
name: ${{ matrix.go }} on ${{ matrix.os }}
steps:

- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

- id: go-cache-paths
run: |
echo "::set-output name=go-build::$(go env GOCACHE)"
echo "::set-output name=go-mod::$(go env GOMODCACHE)"
- run: echo ${{ steps.go-cache-paths.outputs.go-build }}
- run: echo ${{ steps.go-cache-paths.outputs.go-mod }}

- name: Go Build Cache
uses: actions/cache@v2
with:
path: ${{ steps.go-cache-paths.outputs.go-build }}
key: ${{ runner.os }}-go-${{ matrix.go }}-build-${{ hashFiles('**/go.sum') }}

- name: Go Mod Cache (go>=1.15)
uses: actions/cache@v2
with:
path: ${{ steps.go-cache-paths.outputs.go-mod }}
key: ${{ runner.os }}-go-${{ matrix.go }}-mod-${{ hashFiles('**/go.sum') }}
if: matrix.go != '1.14'


- uses: actions/checkout@v2

- run: go mod download && go mod tidy && go mod verify
- if: runner.os == 'Linux'
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]

- run: go vet ./...
- if: runner.os == 'Linux'
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]

- run: go fmt ./...
- if: runner.os == 'Linux'
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]

- run: go test -tags santhoshtekuri ./...
- run: go test -tags santhoshtekuri -v -run TestRaceyPatternSchema -race ./...
env:
CGO_ENABLED: '1'
- if: runner.os == 'Linux'
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]
- run: |
cp openapi3/testdata/load_with_go_embed_test.go openapi3/
cat go.mod | sed 's%go 1.14%go 1.16%' >gomod && mv gomod go.mod
go test -tags santhoshtekuri ./...
if: matrix.go != '1.14'


- if: runner.os == 'Linux'
name: Errors must not be capitalized https://github.com/golang/go/wiki/CodeReviewComments#error-strings
run: |
! git grep -E '(fmt|errors)[^(]+\(.[A-Z]'

- if: runner.os == 'Linux'
name: Did you mean %q
run: |
! git grep -E "'[%].'"
88 changes: 88 additions & 0 deletions .github/workflows/go-xeiuupv.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: go-xeipuuv
on:
pull_request:
push:

jobs:
build-and-test:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GO111MODULE: 'on'
CGO_ENABLED: '0'
strategy:
fail-fast: true
matrix:
go: ['1.14', '1.x']
# Locked at https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on
os:
- ubuntu-20.04
- windows-2019
- macos-10.15
runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash
name: ${{ matrix.go }} on ${{ matrix.os }}
steps:

- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

- id: go-cache-paths
run: |
echo "::set-output name=go-build::$(go env GOCACHE)"
echo "::set-output name=go-mod::$(go env GOMODCACHE)"
- run: echo ${{ steps.go-cache-paths.outputs.go-build }}
- run: echo ${{ steps.go-cache-paths.outputs.go-mod }}

- name: Go Build Cache
uses: actions/cache@v2
with:
path: ${{ steps.go-cache-paths.outputs.go-build }}
key: ${{ runner.os }}-go-${{ matrix.go }}-build-${{ hashFiles('**/go.sum') }}

- name: Go Mod Cache (go>=1.15)
uses: actions/cache@v2
with:
path: ${{ steps.go-cache-paths.outputs.go-mod }}
key: ${{ runner.os }}-go-${{ matrix.go }}-mod-${{ hashFiles('**/go.sum') }}
if: matrix.go != '1.14'


- uses: actions/checkout@v2

- run: go mod download && go mod tidy && go mod verify
- if: runner.os == 'Linux'
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]

- run: go vet ./...
- if: runner.os == 'Linux'
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]

- run: go fmt ./...
- if: runner.os == 'Linux'
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]

- run: go test -tags xeipuuv ./...
- run: go test -tags xeipuuv -v -run TestRaceyPatternSchema -race ./...
env:
CGO_ENABLED: '1'
- if: runner.os == 'Linux'
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]
- run: |
cp openapi3/testdata/load_with_go_embed_test.go openapi3/
cat go.mod | sed 's%go 1.14%go 1.16%' >gomod && mv gomod go.mod
go test -tags xeipuuv ./...
if: matrix.go != '1.14'


- if: runner.os == 'Linux'
name: Errors must not be capitalized https://github.com/golang/go/wiki/CodeReviewComments#error-strings
run: |
! git grep -E '(fmt|errors)[^(]+\(.[A-Z]'

- if: runner.os == 'Linux'
name: Did you mean %q
run: |
! git grep -E "'[%].'"
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ jobs:
- if: runner.os == 'Linux'
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]

- run: go test ./...
- run: go test -v -run TestRaceyPatternSchema -race ./...
- run: go test -tags legacy ./...
- run: go test -tags legacy -v -run TestRaceyPatternSchema -race ./...
env:
CGO_ENABLED: '1'
- if: runner.os == 'Linux'
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]
- run: |
cp openapi3/testdata/load_with_go_embed_test.go openapi3/
cat go.mod | sed 's%go 1.14%go 1.16%' >gomod && mv gomod go.mod
go test ./...
go test -tags legacy ./...
if: matrix.go != '1.14'


Expand Down
7 changes: 7 additions & 0 deletions WIP
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
https://pkg.go.dev/github.com/santhosh-tekuri/jsonschema/v4#Compiler
(*Compiler).AddResource and (*Compiler).Compile() both require filenames / URLs
How to map that to components $ref?

https://pkg.go.dev/github.com/qri-io/jsonschema?utm_source=godoc
1 schema = 1 json.Unmarshal
How to add multiple component schemas?
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ require (
github.com/ghodss/yaml v1.0.0
github.com/go-openapi/jsonpointer v0.19.5
github.com/gorilla/mux v1.8.0
github.com/santhosh-tekuri/jsonschema/v4 v4.0.0
github.com/stretchr/testify v1.5.1
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonschema v1.2.0
gopkg.in/yaml.v2 v2.3.0 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,19 @@ github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/santhosh-tekuri/jsonschema/v4 v4.0.0 h1:5yvbsmEfOQE2+YuRudUhXPzMQK6P74AR3LafM5zrzf0=
github.com/santhosh-tekuri/jsonschema/v4 v4.0.0/go.mod h1:P2UNYJ2xnL4zRyQ2obmD35HiejSJhHTKwMcckhPucG4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
6 changes: 4 additions & 2 deletions openapi3/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/go-openapi/jsonpointer"
)

// Callbacks represents components' callback mapping
type Callbacks map[string]*CallbackRef

var _ jsonpointer.JSONPointable = (*Callbacks)(nil)
Expand All @@ -26,8 +27,9 @@ func (c Callbacks) JSONLookup(token string) (interface{}, error) {
// Callback is specified by OpenAPI/Swagger standard version 3.0.
type Callback map[string]*PathItem

func (value Callback) Validate(ctx context.Context) error {
for _, v := range value {
// Validate goes through the receiver value and its descendants and errors on any non compliance to the OpenAPIv3 specification.
func (cb Callback) Validate(ctx context.Context) error {
for _, v := range cb {
if err := v.Validate(ctx); err != nil {
return err
}
Expand Down
17 changes: 1 addition & 16 deletions openapi3/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package openapi3

import (
"context"
"fmt"
"regexp"

"github.com/getkin/kin-openapi/jsoninfo"
)
Expand Down Expand Up @@ -34,6 +32,7 @@ func (components *Components) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, components)
}

// Validate goes through the receiver value and its descendants and errors on any non compliance to the OpenAPIv3 specification.
func (components *Components) Validate(ctx context.Context) (err error) {
for k, v := range components.Schemas {
if err = ValidateIdentifier(k); err != nil {
Expand Down Expand Up @@ -91,17 +90,3 @@ func (components *Components) Validate(ctx context.Context) (err error) {

return
}

const identifierPattern = `^[a-zA-Z0-9._-]+$`

// IdentifierRegExp verifies whether Component object key matches 'identifierPattern' pattern, according to OapiAPI v3.x.0.
// Hovever, to be able supporting legacy OpenAPI v2.x, there is a need to customize above pattern in orde not to fail
// converted v2-v3 validation
var IdentifierRegExp = regexp.MustCompile(identifierPattern)

func ValidateIdentifier(value string) error {
if IdentifierRegExp.MatchString(value) {
return nil
}
return fmt.Errorf("identifier %q is not supported by OpenAPIv3 standard (regexp: %q)", value, identifierPattern)
}
5 changes: 3 additions & 2 deletions openapi3/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ func (content Content) Get(mime string) *MediaType {
return content["*/*"]
}

func (value Content) Validate(ctx context.Context) error {
for _, v := range value {
// Validate goes through the receiver value and its descendants and errors on any non compliance to the OpenAPIv3 specification.
func (content Content) Validate(ctx context.Context) error {
for _, v := range content {
// Validate MediaType
if err := v.Validate(ctx); err != nil {
return err
Expand Down
11 changes: 6 additions & 5 deletions openapi3/discriminator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ type Discriminator struct {
Mapping map[string]string `json:"mapping,omitempty" yaml:"mapping,omitempty"`
}

func (value *Discriminator) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(value)
func (discr *Discriminator) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(discr)
}

func (value *Discriminator) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, value)
func (discr *Discriminator) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, discr)
}

func (value *Discriminator) Validate(ctx context.Context) error {
// Validate goes through the receiver value and its descendants and errors on any non compliance to the OpenAPIv3 specification.
func (discr *Discriminator) Validate(ctx context.Context) error {
return nil
}
9 changes: 5 additions & 4 deletions openapi3/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ func (encoding *Encoding) SerializationMethod() *SerializationMethod {
return sm
}

func (value *Encoding) Validate(ctx context.Context) error {
if value == nil {
// Validate goes through the receiver value and its descendants and errors on any non compliance to the OpenAPIv3 specification.
func (encoding *Encoding) Validate(ctx context.Context) error {
if encoding == nil {
return nil
}
for k, v := range value.Headers {
for k, v := range encoding.Headers {
if err := ValidateIdentifier(k); err != nil {
return nil
}
Expand All @@ -75,7 +76,7 @@ func (value *Encoding) Validate(ctx context.Context) error {
}

// Validate a media types's serialization method.
sm := value.SerializationMethod()
sm := encoding.SerializationMethod()
switch {
case sm.Style == SerializationForm && sm.Explode,
sm.Style == SerializationForm && !sm.Explode,
Expand Down
8 changes: 4 additions & 4 deletions openapi3/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ func TestEncodingJSON(t *testing.T) {
require.NotEmpty(t, data)

t.Log("Unmarshal *openapi3.Encoding from JSON")
docA := &Encoding{}
err = json.Unmarshal(encodingJSON, &docA)
enc := &Encoding{}
err = json.Unmarshal(encodingJSON, &enc)
require.NoError(t, err)
require.NotEmpty(t, data)

t.Log("Validate *openapi3.Encoding")
err = docA.Validate(context.Background())
err = enc.Validate(context.Background())
require.NoError(t, err)

t.Log("Ensure representations match")
dataA, err := json.Marshal(docA)
dataA, err := json.Marshal(enc)
require.NoError(t, err)
require.JSONEq(t, string(data), string(encodingJSON))
require.JSONEq(t, string(data), string(dataA))
Expand Down
5 changes: 3 additions & 2 deletions openapi3/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (example *Example) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, example)
}

func (value *Example) Validate(ctx context.Context) error {
return nil // TODO
// Validate goes through the receiver value and its descendants and errors on any non compliance to the OpenAPIv3 specification.
func (example *Example) Validate(ctx context.Context) error {
return nil // TODO https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#example-object
}
Loading