Skip to content

Commit

Permalink
[codegen] Update the internal fork of deepmap/oapi-codegen (#343)
Browse files Browse the repository at this point in the history
Update and re-patch the internal fork of deepmap/oapi-codegen to use
commit `620da6b569e3882bb4401d9a16b9982c3a7f88cb`, which uses
`getkin/[email protected]`, the same version as
`grafana/grafana-plugin-sdk-go`, allowing the replace directive to be
removed from `go.mod` and `go.work`. This should allow subsequent
releases to be installed with `go install`.
  • Loading branch information
IfSentient authored Jul 26, 2024
1 parent f50abdc commit b0dbab5
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 308 deletions.
14 changes: 6 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
module github.com/grafana/grafana-app-sdk

go 1.22.2

// Required for compatibility with grafana/grafana-plugin-sdk-go & grafana/thema
replace github.com/getkin/kin-openapi => github.com/getkin/kin-openapi v0.123.0
go 1.22.4

require (
cuelang.org/go v0.5.0
github.com/dave/dst v0.27.3
github.com/getkin/kin-openapi v0.123.0
github.com/getkin/kin-openapi v0.124.0
github.com/grafana/codejen v0.0.3
github.com/grafana/cuetsy v0.1.10
github.com/grafana/thema v0.0.0-20230511182720-3146087fcc26
github.com/hashicorp/go-multierror v1.1.1
github.com/matryer/moq v0.3.4
github.com/oapi-codegen/oapi-codegen/v2 v2.3.0
github.com/prometheus/client_golang v1.19.1
github.com/puzpuzpuz/xsync/v2 v2.5.1
github.com/spf13/cobra v1.8.1
Expand Down Expand Up @@ -52,9 +50,9 @@ require (
github.com/getsentry/sentry-go v0.12.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
Expand All @@ -65,7 +63,7 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/invopop/yaml v0.3.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand Down
25 changes: 14 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gavv/httpexpect v2.0.0+incompatible/go.mod h1:x+9tiU1YnrOvnB725RkpoLv1M62hOWzwo5OXotisrKc=
github.com/getkin/kin-openapi v0.123.0 h1:zIik0mRwFNLyvtXK274Q6ut+dPh6nlxBp0x7mNrPhs8=
github.com/getkin/kin-openapi v0.123.0/go.mod h1:wb1aSZA/iWmorQP9KTAS/phLj/t17B5jT7+fS8ed9NM=
github.com/getkin/kin-openapi v0.124.0 h1:VSFNMB9C9rTKBnQ/fpyDU8ytMTr4dWI9QovSKj9kz/M=
github.com/getkin/kin-openapi v0.124.0/go.mod h1:wb1aSZA/iWmorQP9KTAS/phLj/t17B5jT7+fS8ed9NM=
github.com/getkin/kin-openapi v0.126.0 h1:c2cSgLnAsS0xYfKsgt5oBV6MYRM/giU8/RtwUY4wyfY=
github.com/getkin/kin-openapi v0.126.0/go.mod h1:7mONz8IwmSRg6RttPu6v8U/OJ+gr+J99qSFNjPGSQqw=
github.com/getsentry/sentry-go v0.12.0 h1:era7g0re5iY13bHSdN/xMkyV+5zZppjRVQhZrXCaEIk=
github.com/getsentry/sentry-go v0.12.0/go.mod h1:NSap0JBYWzHND8oMbyi0+XZhUalc1TBdRL1M71JZW2c=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
Expand All @@ -77,13 +79,13 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AEU963A2AYjv4d1V5eVL1CQbEJq6aCNHDDjibzu8=
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q=
github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs=
github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ=
github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY=
github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE=
github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k=
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/go-openapi/swag v0.22.8 h1:/9RjDSQ0vbFR+NyjGMkFTsA1IA0fmhKSThmfGZjicbw=
github.com/go-openapi/swag v0.22.8/go.mod h1:6QT22icPLEqAM/z/TChgb4WAveCHF92+2gF0CNjHpPI=
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
Expand Down Expand Up @@ -156,8 +158,8 @@ github.com/imkira/go-interpol v1.1.0/go.mod h1:z0h2/2T3XF8kyEPpRgJ3kmNv+C43p+I/C
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=
github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81KkeA=
github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbkg6SAB4att1aAwTmVIx/5gCVqeyUdI=
github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0=
github.com/iris-contrib/jade v1.1.3/go.mod h1:H/geBymxJhShH5kecoiOCSssPX7QWYH7UaeZTSWddIk=
Expand Down Expand Up @@ -236,6 +238,8 @@ github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5Vgl
github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzEE/Zbp4w=
github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/oapi-codegen/oapi-codegen/v2 v2.3.0 h1:rICjNsHbPP1LttefanBPnwsSwl09SqhCO7Ee623qR84=
github.com/oapi-codegen/oapi-codegen/v2 v2.3.0/go.mod h1:4k+cJeSq5ntkwlcpQSxLxICCxQzCL772o30PxdibRt4=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.3/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
Expand Down Expand Up @@ -308,8 +312,8 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.6.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w=
Expand Down Expand Up @@ -512,7 +516,6 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
Expand Down
5 changes: 1 addition & 4 deletions go.work
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
go 1.22.2
go 1.22.4

use (
.
./plugin
)

// Required for compatibility with grafana/grafana-plugin-sdk-go & grafana/thema
replace github.com/getkin/kin-openapi => github.com/getkin/kin-openapi v0.123.0
3 changes: 3 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab h1:xveKWz2iaueeTaUgdetzel+U7exyigDYBryyVfV/rZk=
github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ=
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
Expand Down Expand Up @@ -155,6 +157,7 @@ github.com/hydrogen18/memlistener v0.0.0-20200120041712-dcc25e7acd91 h1:KyZDvZ/G
github.com/imkira/go-interpol v1.1.0 h1:KIiKr0VSG2CUW1hl1jpiyuzuJeKUUpC8iM1AIE7N1Vk=
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=
github.com/iris-contrib/blackfriday v2.0.0+incompatible h1:o5sHQHHm0ToHUlAJSTjW9UWicjJSDDauOOQ2AHuIVp4=
github.com/iris-contrib/go.uuid v2.0.0+incompatible h1:XZubAYg61/JwnJNbZilGjf3b3pB80+OQg2qf6c8BfWE=
github.com/iris-contrib/jade v1.1.3 h1:p7J/50I0cjo0wq/VWVCDFd8taPJbuFC+bq23SniRFX0=
Expand Down
2 changes: 1 addition & 1 deletion internal/deepmap/UPDATING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Updating this fork of deepmap/oapi-codegen

The contents of the `oapi-codegen` are copied from the `deepmap/oapi-codegen@v2.1.0`, with the following changes:
The contents of the `oapi-codegen` are copied from the `deepmap/oapi-codegen@620da6b569e3882bb4401d9a16b9982c3a7f88cb`, with the following changes:
* All packages except `pkg/codegen` and `pkg/util` have been removed, as they are not needed for go types generation.
* Package refs have been changed from `github.com/deepmap/oapi-codegen` to `github.com/grafana/thema/internal/deepmap/oapi-codegen`
* The contents of [this PR](https://github.com/deepmap/oapi-codegen/pull/717) in deepmap/oapi-codegen have been played onto this fork
Expand Down
10 changes: 1 addition & 9 deletions internal/deepmap/diff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ diff /Users/austinpond/github/oapi-codegen/pkg/codegen/codegen_test.go internal/
< "github.com/deepmap/oapi-codegen/v2/pkg/util"
---
> "github.com/grafana/grafana-app-sdk/internal/deepmap/oapi-codegen/pkg/util"
18c18
< remoteRefImport = `github.com/deepmap/oapi-codegen/v2/examples/petstore-expanded`
---
> remoteRefImport = `github.com/grafana/grafana-app-sdk/internal/deepmap/oapi-codegen/examples/petstore-expanded`
201c201
< assert.Contains(t, code, `externalRef0 "github.com/deepmap/oapi-codegen/v2/examples/petstore-expanded"`)
---
> assert.Contains(t, code, `externalRef0 "github.com/grafana/grafana-app-sdk/internal/deepmap/oapi-codegen/examples/petstore-expanded"`)
diff /Users/austinpond/github/oapi-codegen/pkg/codegen/merge_schemas.go internal/deepmap/oapi-codegen/pkg/codegen/merge_schemas.go
13c13
< func MergeSchemas(allOf []*openapi3.SchemaRef, path []string) (Schema, error) {
Expand Down Expand Up @@ -52,7 +44,7 @@ diff /Users/austinpond/github/oapi-codegen/pkg/codegen/operations.go internal/de
---
> "github.com/grafana/grafana-app-sdk/internal/deepmap/oapi-codegen/pkg/util"
diff /Users/austinpond/github/oapi-codegen/pkg/codegen/schema.go internal/deepmap/oapi-codegen/pkg/codegen/schema.go
277c277
272c272
< mergedSchema, err := MergeSchemas(schema.AllOf, path)
---
> mergedSchema, err := MergeSchemas(schema, path)
Expand Down
17 changes: 8 additions & 9 deletions internal/deepmap/oapi-codegen/pkg/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ func GenerateTypesForSchemas(t *template.Template, schemas map[string]*openapi3.
Schema: goSchema,
})

types = append(types, goSchema.GetAdditionalTypeDefs()...)
types = append(types, goSchema.AdditionalTypes...)
}
return types, nil
}
Expand All @@ -546,7 +546,7 @@ func GenerateTypesForSchemas(t *template.Template, schemas map[string]*openapi3.
// components/parameters section of the Swagger spec.
func GenerateTypesForParameters(t *template.Template, params map[string]*openapi3.ParameterRef) ([]TypeDefinition, error) {
var types []TypeDefinition
for _, paramName := range SortedParameterKeys(params) {
for _, paramName := range SortedMapKeys(params) {
paramOrRef := params[paramName]

goType, err := paramToGoType(paramOrRef.Value, nil)
Expand Down Expand Up @@ -584,7 +584,7 @@ func GenerateTypesForParameters(t *template.Template, params map[string]*openapi
func GenerateTypesForResponses(t *template.Template, responses openapi3.ResponseBodies) ([]TypeDefinition, error) {
var types []TypeDefinition

for _, responseName := range SortedResponsesKeys(responses) {
for _, responseName := range SortedMapKeys(responses) {
responseOrRef := responses[responseName]

// We have to generate the response object. We're only going to
Expand All @@ -599,8 +599,8 @@ func GenerateTypesForResponses(t *template.Template, responses openapi3.Response
}
}

sortedContentKeys := SortedContentKeys(response.Content)
for _, mediaType := range sortedContentKeys {
SortedMapKeys := SortedMapKeys(response.Content)
for _, mediaType := range SortedMapKeys {
response := response.Content[mediaType]
if !util.IsMediaTypeJson(mediaType) {
continue
Expand Down Expand Up @@ -646,7 +646,7 @@ func GenerateTypesForResponses(t *template.Template, responses openapi3.Response
func GenerateTypesForRequestBodies(t *template.Template, bodies map[string]*openapi3.RequestBodyRef) ([]TypeDefinition, error) {
var types []TypeDefinition

for _, requestBodyName := range SortedRequestBodyKeys(bodies) {
for _, requestBodyName := range SortedMapKeys(bodies) {
requestBodyRef := bodies[requestBodyName]

// As for responses, we will only generate Go code for JSON bodies,
Expand Down Expand Up @@ -1088,16 +1088,15 @@ func GoSchemaImports(schemas ...*openapi3.SchemaRef) (map[string]goImport, error
schemaVal := sref.Value

t := schemaVal.Type
switch t {
case "", "object":
if t.Slice() == nil || t.Is("object") {
for _, v := range schemaVal.Properties {
imprts, err := GoSchemaImports(v)
if err != nil {
return nil, err
}
MergeImports(res, imprts)
}
case "array":
} else if t.Is("array") {
imprts, err := GoSchemaImports(schemaVal.Items)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions internal/deepmap/oapi-codegen/pkg/codegen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
const (
remoteRefFile = `https://raw.githubusercontent.com/deepmap/oapi-codegen/master/examples/petstore-expanded` +
`/petstore-expanded.yaml`
remoteRefImport = `github.com/grafana/grafana-app-sdk/internal/deepmap/oapi-codegen/examples/petstore-expanded`
remoteRefImport = `github.com/deepmap/oapi-codegen/v2/examples/petstore-expanded`
)

func TestExampleOpenAPICodeGeneration(t *testing.T) {
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestRemoteExternalReference(t *testing.T) {
assert.Contains(t, code, "package api")

// Check import
assert.Contains(t, code, `externalRef0 "github.com/grafana/grafana-app-sdk/internal/deepmap/oapi-codegen/examples/petstore-expanded"`)
assert.Contains(t, code, `externalRef0 "github.com/deepmap/oapi-codegen/v2/examples/petstore-expanded"`)

// Check generated oneOf structure:
assert.Contains(t, code, `
Expand Down
41 changes: 27 additions & 14 deletions internal/deepmap/oapi-codegen/pkg/codegen/merge_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,14 @@ func mergeAllOf(allOf []*openapi3.SchemaRef) (openapi3.Schema, error) {
// all of whose fields are composed.
func mergeOpenapiSchemas(s1, s2 openapi3.Schema, allOf bool) (openapi3.Schema, error) {
var result openapi3.Schema
if s1.Extensions != nil || s2.Extensions != nil {
result.Extensions = make(map[string]interface{})
if s1.Extensions != nil {
for k, v := range s1.Extensions {
result.Extensions[k] = v
}
}
if s2.Extensions != nil {
for k, v := range s2.Extensions {
// TODO: Check for collisions
result.Extensions[k] = v
}
}

result.Extensions = make(map[string]interface{})
for k, v := range s1.Extensions {
result.Extensions[k] = v
}
for k, v := range s2.Extensions {
// TODO: Check for collisions
result.Extensions[k] = v
}

result.OneOf = append(s1.OneOf, s2.OneOf...)
Expand All @@ -124,7 +119,7 @@ func mergeOpenapiSchemas(s1, s2 openapi3.Schema, allOf bool) (openapi3.Schema, e

result.AllOf = append(s1.AllOf, s2.AllOf...)

if s1.Type != "" && s2.Type != "" && s1.Type != s2.Type {
if s1.Type.Slice() != nil && s2.Type.Slice() != nil && !equalTypes(s1.Type, s2.Type) {
return openapi3.Schema{}, errors.New("can not merge incompatible types")
}
result.Type = s1.Type
Expand Down Expand Up @@ -233,3 +228,21 @@ func mergeOpenapiSchemas(s1, s2 openapi3.Schema, allOf bool) (openapi3.Schema, e

return result, nil
}

func equalTypes(t1 *openapi3.Types, t2 *openapi3.Types) bool {
s1 := t1.Slice()
s2 := t2.Slice()

if len(s1) != len(s2) {
return false
}

// NOTE that ideally we'd use `slices.Equal` but as we're currently supporting Go 1.20+, we can't use it (yet https://github.com/deepmap/oapi-codegen/issues/1634)
for i := range s1 {
if s1[i] != s2[i] {
return false
}
}

return true
}
Loading

0 comments on commit b0dbab5

Please sign in to comment.