Skip to content

Commit

Permalink
refactor traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
jxsl13 committed Jul 29, 2023
1 parent cdfc7d6 commit 7926aea
Show file tree
Hide file tree
Showing 18 changed files with 472 additions and 84 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand All @@ -36,7 +35,7 @@ require (
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.8.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/getkin/kin-openapi v0.118.0 h1:z43njxPmJ7TaPpMSCQb7PN0dEYno4tyBPQcrFdHoLuM=
github.com/getkin/kin-openapi v0.118.0/go.mod h1:l5e9PaFUo9fyLJCPGQeXI2ML8c3P8BHOEV2VaAVf/pc=
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonpointer v0.20.0 h1:ESKJdU9ASRfaPNOPRx12IUyA1vn3R9GiE3KYD14BXdQ=
github.com/go-openapi/jsonpointer v0.20.0/go.mod h1:6PGzBjjIIumbLYysB73Klnms1mwnU4G3YHOECG3CedA=
Expand Down Expand Up @@ -43,7 +40,6 @@ github.com/knadh/koanf/providers/structs v0.1.0 h1:wJRteCNn1qvLtE5h8KQBvLJovidSd
github.com/knadh/koanf/providers/structs v0.1.0/go.mod h1:sw2YZ3txUcqA3Z27gPlmmBzWn1h8Nt9O6EP/91MkcWE=
github.com/knadh/koanf/v2 v2.0.1 h1:1dYGITt1I23x8cfx8ZnldtezdyaZtfAuRtIFOiRzK7g=
github.com/knadh/koanf/v2 v2.0.1/go.mod h1:ZeiIlIDXTE7w1lMT6UVcNiRAS2/rCeLn/GdLNvY1Dus=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -71,6 +67,8 @@ github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
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/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
Expand All @@ -89,8 +87,6 @@ github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
Expand Down
30 changes: 30 additions & 0 deletions names/deduplicate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package names

import "strings"

func Deduplicate(names []string) []string {
dups := make(map[int]bool, len(names)-1)
for i := range names {
for j := range names {
if i == j {
continue
}
ni := strings.ToLower(names[i])
nj := strings.ToLower(names[j])
if strings.Contains(ni, nj) {
dups[j] = true
} else if strings.Contains(nj, ni) {
dups[i] = true
}
}
}

result := make([]string, 0, len(names)-len(dups))
for idx, name := range names {
if !dups[idx] {
result = append(result, name)
}
}

return result
}
27 changes: 27 additions & 0 deletions names/deduplicate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package names_test

import (
"testing"

"github.com/jxsl13/openapi-typegen/names"
"github.com/stretchr/testify/assert"
)

func TestDeduplicate(t *testing.T) {
in := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
expected := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
out := names.Deduplicate(in)

assert.Equal(t, expected, out)

in = []string{"a", "b", "c", "d", "ef", "f", "g", "h", "i", "j"}
expected = []string{"a", "b", "c", "d", "ef", "g", "h", "i", "j"}
out = names.Deduplicate(in)
assert.Equal(t, expected, out)

in = []string{"a", "bd", "c", "d", "ef", "f", "g", "gh", "i", "j"}
expected = []string{"a", "bd", "c", "ef", "gh", "i", "j"}
out = names.Deduplicate(in)
assert.Equal(t, expected, out)

}
164 changes: 164 additions & 0 deletions testdata/004_callbacks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
openapi: 3.0.1

info:
title: OpenAPI-CodeGen Test
description: 'This is a test OpenAPI Spec'
version: 1.0.0

servers:
- url: https://test.oapi-codegen.com/v2
- url: http://test.oapi-codegen.com/v2

paths:
/test:
get:
operationId: doesNothing
summary: does nothing
tags: [nothing]
responses:
default:
description: returns nothing
content:
application/json:
schema:
type: object
components:
schemas:
Object1:
type: object
properties:
object:
$ref: "#/components/schemas/Object2"
Object2:
type: object
properties:
object:
$ref: "#/components/schemas/Object3"
Object3:
type: object
properties:
object:
$ref: "#/components/schemas/Object4"
Object4:
type: object
properties:
object:
$ref: "#/components/schemas/Object5"
Object5:
type: object
properties:
object:
$ref: "#/components/schemas/Object6"
Object6:
type: object
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
Error:
required:
- code
- message
properties:
code:
type: integer
format: int32
description: Error code
message:
type: string
description: Error message
parameters:
offsetParam:
name: offset
in: query
description: Number of items to skip before returning the results.
required: false
schema:
type: integer
format: int32
minimum: 0
default: 0
securitySchemes:
BasicAuth:
type: http
scheme: basic
BearerAuth:
type: http
scheme: bearer
requestBodies:
PetBody:
description: A JSON object containing pet information
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
responses:
NotFound:
description: The specified resource was not found
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
Unauthorized:
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
headers:
X-RateLimit-Limit:
schema:
type: integer
description: Request limit per hour.
X-RateLimit-Remaining:
schema:
type: integer
description: The number of requests left for the time window.
X-RateLimit-Reset:
schema:
type: string
format: date-time
description: The UTC date/time at which the current rate limit window resets
examples:
objectExample:
value:
id: 1
name: new object
summary: A sample object
links:
GetUserByUserId:
description: >
The id value returned in the response can be used as
the userId parameter in GET /users/{userId}.
operationId: getUser
parameters:
userId: '$response.body#/id'
callbacks:
MyCallback:
'{$request.body#/callbackUrl}':
post:
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: Some event happened
required:
- message
responses:
'200':
description: Your server returns this code if it accepts the callback
4 changes: 2 additions & 2 deletions traverse/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package traverse
import "github.com/getkin/kin-openapi/openapi3"

// Callback traverses all unique non-reference schemas in the given callback.
func Callback(callback *openapi3.CallbackRef, visitor SchemaVisitor, levelNames ...string) error {
func Callback(callback *openapi3.CallbackRef, visitor SchemaVisitor, levelNames map[string][]string) error {
if callback == nil {
return nil
}
Expand All @@ -23,7 +23,7 @@ func Callback(callback *openapi3.CallbackRef, visitor SchemaVisitor, levelNames
continue
}

err = PathItem(pathItem, visitor, append(levelNames, callbackName)...)
err = PathItem(pathItem, visitor, add(levelNames, NameKey, callbackName))
if err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions traverse/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package traverse
import "github.com/getkin/kin-openapi/openapi3"

// Components traverses the given components and all unique non-reference schemas in it.
func Components(components *openapi3.Components, visitor SchemaVisitor, levelNames ...string) error {
func Components(components *openapi3.Components, visitor SchemaVisitor, levelNames map[string][]string) error {
if components == nil {
return nil
}
Expand All @@ -18,7 +18,7 @@ func Components(components *openapi3.Components, visitor SchemaVisitor, levelNam
if schema.Ref != "" {
continue
}
err = visitor(schema, append(levelNames, schemaName, SchemaSuffix)...)
err = visitor(schema, add(levelNames, NameKey, schemaName, TypeKey, SchemaType))
if err != nil {
return err
}
Expand All @@ -33,7 +33,7 @@ func Components(components *openapi3.Components, visitor SchemaVisitor, levelNam
continue
}

err = Header(header, visitor, append(levelNames, headerName)...)
err = Header(header, visitor, add(levelNames, NameKey, headerName))
if err != nil {
return err
}
Expand All @@ -48,7 +48,7 @@ func Components(components *openapi3.Components, visitor SchemaVisitor, levelNam
if parameter.Ref != "" {
continue
}
err = Parameter(parameter, visitor, append(levelNames, parameterName)...)
err = Parameter(parameter, visitor, add(levelNames, NameKey, parameterName))
if err != nil {
return err
}
Expand All @@ -62,7 +62,7 @@ func Components(components *openapi3.Components, visitor SchemaVisitor, levelNam
if requestBody.Ref != "" {
continue
}
err = RequestBody(requestBody, visitor, append(levelNames, requestBodyName)...)
err = RequestBody(requestBody, visitor, add(levelNames, NameKey, requestBodyName))
if err != nil {
return err
}
Expand All @@ -76,7 +76,7 @@ func Components(components *openapi3.Components, visitor SchemaVisitor, levelNam
if response.Ref != "" {
continue
}
err = Response(response, visitor, append(levelNames, responseName)...)
err = Response(response, visitor, add(levelNames, NameKey, responseName))
if err != nil {
return err
}
Expand All @@ -90,7 +90,7 @@ func Components(components *openapi3.Components, visitor SchemaVisitor, levelNam
if callback.Ref != "" {
continue
}
err = Callback(callback, visitor, append(levelNames, callbackName)...)
err = Callback(callback, visitor, add(levelNames, NameKey, callbackName))
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions traverse/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package traverse
import "github.com/getkin/kin-openapi/openapi3"

// Header traverses the given header and all unique non-reference schemas in it.
func Header(header *openapi3.HeaderRef, visitor SchemaVisitor, levelNames ...string) error {
func Header(header *openapi3.HeaderRef, visitor SchemaVisitor, levelNames map[string][]string) error {
if header == nil {
return nil
}
Expand All @@ -13,8 +13,7 @@ func Header(header *openapi3.HeaderRef, visitor SchemaVisitor, levelNames ...str
if header.Value == nil {
return nil
}
if header.Value.Schema == nil {
return nil
}
return visitor(header.Value.Schema, append(levelNames, HeaderSuffix)...)

// we want to handle component header definitions like any other parameter
return ParameterSchema(&header.Value.Parameter, visitor, add(levelNames, InKey, openapi3.ParameterInHeader))
}
4 changes: 2 additions & 2 deletions traverse/media_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package traverse
import "github.com/getkin/kin-openapi/openapi3"

// MediaType traverses the given media type and all unique non-reference schemas in it.
func MediaType(mediaType *openapi3.MediaType, visitor SchemaVisitor, levelNames ...string) error {
func MediaType(mediaType *openapi3.MediaType, visitor SchemaVisitor, levelNames map[string][]string) error {
if mediaType == nil {
return nil
}
Expand All @@ -15,5 +15,5 @@ func MediaType(mediaType *openapi3.MediaType, visitor SchemaVisitor, levelNames
return nil
}

return visitor(mediaType.Schema, levelNames...)
return visitor(mediaType.Schema, levelNames)
}
Loading

0 comments on commit 7926aea

Please sign in to comment.