Skip to content

Commit 42d87c7

Browse files
Respect setting of extension property x-go-type-skip-optional-pointer defined on referenced types
1 parent 62fec2f commit 42d87c7

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

pkg/codegen/schema.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,20 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
260260

261261
schema := sref.Value
262262

263+
// Check x-go-type-skip-optional-pointer, which will override if the type
264+
// should be a pointer or not when the field is optional.
265+
// NOTE skipOptionalPointer will be defaulted to the global value, but can be overridden on a per-type/-field basis
266+
// Check x-go-type-skip-optional-pointer, which will override if the type
267+
// should be a pointer or not when the field is optional.
268+
skipOptionalPointer := globalState.options.OutputOptions.PreferSkipOptionalPointer
269+
if extension, ok := schema.Extensions[extPropGoTypeSkipOptionalPointer]; ok {
270+
var err error
271+
skipOptionalPointer, err = extParsePropGoTypeSkipOptionalPointer(extension)
272+
if err != nil {
273+
return Schema{}, fmt.Errorf("invalid value for %q: %w", extPropGoTypeSkipOptionalPointer, err)
274+
}
275+
}
276+
263277
// If Ref is set on the SchemaRef, it means that this type is actually a reference to
264278
// another type. We're not de-referencing, so simply use the referenced type.
265279
if IsGoTypeReference(sref.Ref) {
@@ -274,15 +288,14 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
274288
Description: schema.Description,
275289
DefineViaAlias: true,
276290
OAPISchema: schema,
277-
SkipOptionalPointer: globalState.options.OutputOptions.PreferSkipOptionalPointer,
291+
SkipOptionalPointer: skipOptionalPointer,
278292
}, nil
279293
}
280294

281295
outSchema := Schema{
282-
Description: schema.Description,
283-
OAPISchema: schema,
284-
// NOTE that SkipOptionalPointer will be defaulted to the global value, but can be overridden on a per-type/-field basis
285-
SkipOptionalPointer: globalState.options.OutputOptions.PreferSkipOptionalPointer,
296+
Description: schema.Description,
297+
OAPISchema: schema,
298+
SkipOptionalPointer: skipOptionalPointer,
286299
}
287300

288301
// AllOf is interesting, and useful. It's the union of a number of other
@@ -298,16 +311,6 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
298311
return mergedSchema, nil
299312
}
300313

301-
// Check x-go-type-skip-optional-pointer, which will override if the type
302-
// should be a pointer or not when the field is optional.
303-
if extension, ok := schema.Extensions[extPropGoTypeSkipOptionalPointer]; ok {
304-
skipOptionalPointer, err := extParsePropGoTypeSkipOptionalPointer(extension)
305-
if err != nil {
306-
return outSchema, fmt.Errorf("invalid value for %q: %w", extPropGoTypeSkipOptionalPointer, err)
307-
}
308-
outSchema.SkipOptionalPointer = skipOptionalPointer
309-
}
310-
311314
// Check x-go-type, which will completely override the definition of this
312315
// schema with the provided type.
313316
if extension, ok := schema.Extensions[extPropGoType]; ok {

0 commit comments

Comments
 (0)