Skip to content

Commit dee66f8

Browse files
committed
chore(tests): add test case for x-go-type and -skip-optional-pointer
As part of future changes, we'll be modifying the way this works, so we should capture the current behaviour.
1 parent e62b976 commit dee66f8

File tree

7 files changed

+152
-3
lines changed

7 files changed

+152
-3
lines changed

internal/test/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/gin-gonic/gin v1.9.1
1010
github.com/go-chi/chi/v5 v5.0.10
1111
github.com/gofiber/fiber/v2 v2.49.1
12+
github.com/google/uuid v1.4.0
1213
github.com/gorilla/mux v1.8.1
1314
github.com/kataras/iris/v12 v12.2.6-0.20230908161203-24ba4e8933b9
1415
github.com/labstack/echo/v4 v4.11.3
@@ -47,7 +48,6 @@ require (
4748
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
4849
github.com/golang/snappy v0.0.4 // indirect
4950
github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12 // indirect
50-
github.com/google/uuid v1.4.0 // indirect
5151
github.com/gorilla/css v1.0.0 // indirect
5252
github.com/invopop/yaml v0.3.1 // indirect
5353
github.com/iris-contrib/schema v0.0.6 // indirect
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package: issue1957
2+
generate:
3+
models: true
4+
output: issue1957.gen.go
5+
output-options:
6+
skip-prune: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package issue1957
2+
3+
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml openapi.yaml

internal/test/issues/issue1957/issue1957.gen.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package issue1957
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/uuid"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestGeneratedCode(t *testing.T) {
11+
t.Run("For an object", func(t *testing.T) {
12+
t.Run("A required field should be a non-pointer", func(t *testing.T) {
13+
theType := TypeWithOptionalField{
14+
AtRequired: uuid.New(),
15+
}
16+
17+
require.NotZero(t, theType.AtRequired)
18+
})
19+
20+
t.Run("An optional field with x-go-type-skip-optional-pointer should be a non-pointer", func(t *testing.T) {
21+
theType := TypeWithOptionalField{
22+
AtRequired: uuid.New(),
23+
}
24+
25+
require.NotZero(t, theType.AtRequired)
26+
})
27+
})
28+
29+
t.Run("For a query parameter", func(t *testing.T) {
30+
// TODO that this is NOT wanted behaviour, but it is our current behaviour
31+
t.Run("An optional field with x-go-type-skip-optional-pointer should be a pointer", func(t *testing.T) {
32+
33+
u := uuid.New()
34+
35+
theType := GetRootParams{
36+
At: &u,
37+
}
38+
39+
require.NotNil(t, theType.At)
40+
require.NotZero(t, *theType.At)
41+
})
42+
})
43+
44+
t.Run("For a field with an AllOf", func(t *testing.T) {
45+
// TODO that this is NOT wanted behaviour, but it is our current behaviour
46+
t.Run("An optional field with x-go-type-skip-optional-pointer should be a pointer", func(t *testing.T) {
47+
48+
u := uuid.New()
49+
50+
theType := TypeWithAllOf{
51+
Id: &u,
52+
}
53+
54+
require.NotNil(t, theType.Id)
55+
require.NotZero(t, *theType.Id)
56+
})
57+
})
58+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: "x-go-type and x-go-type-skip-optional-pointer should be possible to use together"
5+
paths:
6+
/root:
7+
get:
8+
operationId: getRoot
9+
parameters:
10+
- in: query
11+
name: at
12+
schema:
13+
type: string
14+
format: date-time
15+
x-go-type-skip-optional-pointer: true
16+
x-go-type: googleuuid.UUID
17+
x-go-type-import:
18+
path: github.com/google/uuid
19+
name: googleuuid
20+
responses:
21+
"200":
22+
description: Some data
23+
components:
24+
schemas:
25+
TypeWithOptionalField:
26+
type: object
27+
properties:
28+
at:
29+
type: string
30+
x-go-type-skip-optional-pointer: true
31+
x-go-type: googleuuid.UUID
32+
x-go-type-import:
33+
path: github.com/google/uuid
34+
name: googleuuid
35+
at_required:
36+
type: string
37+
x-go-type: googleuuid.UUID
38+
x-go-type-import:
39+
path: github.com/google/uuid
40+
name: googleuuid
41+
required:
42+
- at_required
43+
ID:
44+
type: string
45+
x-go-type: googleuuid.UUID
46+
x-go-type-import:
47+
path: github.com/google/uuid
48+
name: googleuuid
49+
TypeWithAllOf:
50+
type: object
51+
properties:
52+
id:
53+
allOf:
54+
- $ref: '#/components/schemas/ID'
55+
- x-go-type-skip-optional-pointer: true

internal/test/strict-server/stdhttp/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7w
134134
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
135135
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
136136
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
137-
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
138-
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
137+
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
138+
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
139139
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
140140
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
141141
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy