Skip to content

Commit a720a1f

Browse files
Nivljamietanna
authored andcommitted
fix(codegen): allow using x-go-type and x-go-type-skip-optional-pointer together
When using `x-go-type`, `GenerateGoSchema` returns early, meaning that any extensions that are checked after this one are no-op. This PR moves the code for `x-go-type-skip-optional-pointer` higher up, so it can be used alongside `x-go-type`. This corrects this piece of behaviour, and amends our generated code examples to correctly apply `x-go-type-skip-optional-pointer` when using an optional field, or inside an `AllOf`.
1 parent dee66f8 commit a720a1f

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

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

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/test/issues/issue1957/issue1957_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,28 @@ func TestGeneratedCode(t *testing.T) {
2727
})
2828

2929
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) {
30+
t.Run("An optional field with x-go-type-skip-optional-pointer should be a non-pointer", func(t *testing.T) {
3231

3332
u := uuid.New()
3433

3534
theType := GetRootParams{
36-
At: &u,
35+
At: u,
3736
}
3837

39-
require.NotNil(t, theType.At)
40-
require.NotZero(t, *theType.At)
38+
require.NotZero(t, theType.At)
4139
})
4240
})
4341

4442
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) {
43+
t.Run("An optional field with x-go-type-skip-optional-pointer should be a non-pointer", func(t *testing.T) {
4744

4845
u := uuid.New()
4946

5047
theType := TypeWithAllOf{
51-
Id: &u,
48+
Id: u,
5249
}
5350

54-
require.NotNil(t, theType.Id)
55-
require.NotZero(t, *theType.Id)
51+
require.NotZero(t, theType.Id)
5652
})
5753
})
5854
}

pkg/codegen/schema.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,16 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
289289
return mergedSchema, nil
290290
}
291291

292+
// Check x-go-type-skip-optional-pointer, which will override if the type
293+
// should be a pointer or not when the field is optional.
294+
if extension, ok := schema.Extensions[extPropGoTypeSkipOptionalPointer]; ok {
295+
skipOptionalPointer, err := extParsePropGoTypeSkipOptionalPointer(extension)
296+
if err != nil {
297+
return outSchema, fmt.Errorf("invalid value for %q: %w", extPropGoTypeSkipOptionalPointer, err)
298+
}
299+
outSchema.SkipOptionalPointer = skipOptionalPointer
300+
}
301+
292302
// Check x-go-type, which will completely override the definition of this
293303
// schema with the provided type.
294304
if extension, ok := schema.Extensions[extPropGoType]; ok {
@@ -302,16 +312,6 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
302312
return outSchema, nil
303313
}
304314

305-
// Check x-go-type-skip-optional-pointer, which will override if the type
306-
// should be a pointer or not when the field is optional.
307-
if extension, ok := schema.Extensions[extPropGoTypeSkipOptionalPointer]; ok {
308-
skipOptionalPointer, err := extParsePropGoTypeSkipOptionalPointer(extension)
309-
if err != nil {
310-
return outSchema, fmt.Errorf("invalid value for %q: %w", extPropGoTypeSkipOptionalPointer, err)
311-
}
312-
outSchema.SkipOptionalPointer = skipOptionalPointer
313-
}
314-
315315
// Schema type and format, eg. string / binary
316316
t := schema.Type
317317
// Handle objects and empty schemas first as a special case

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