(sref *openapi3.SchemaRef, path []string)
| 324 | } |
| 325 | |
| 326 | func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) { |
| 327 | // Add a fallback value in case the sref is nil. |
| 328 | // i.e. the parent schema defines a type:array, but the array has |
| 329 | // no items defined. Therefore, we have at least valid Go-Code. |
| 330 | if sref == nil { |
| 331 | return Schema{GoType: "interface{}"}, nil |
| 332 | } |
| 333 | |
| 334 | schema := sref.Value |
| 335 | extensions := combinedSchemaExtensions(sref) |
| 336 | |
| 337 | // Check x-go-type-skip-optional-pointer, which will override if the type |
| 338 | // should be a pointer or not when the field is optional. |
| 339 | // NOTE skipOptionalPointer will be defaulted to the global value, but can be overridden on a per-type/-field basis |
| 340 | skipOptionalPointer := globalState.options.OutputOptions.PreferSkipOptionalPointer |
| 341 | if extension, ok := extensions[extPropGoTypeSkipOptionalPointer]; ok { |
| 342 | var err error |
| 343 | skipOptionalPointer, err = extParsePropGoTypeSkipOptionalPointer(extension) |
| 344 | if err != nil { |
| 345 | return Schema{}, fmt.Errorf("invalid value for %q: %w", extPropGoTypeSkipOptionalPointer, err) |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // If Ref is set on the SchemaRef, it means that this type is actually a reference to |
| 350 | // another type. We're not de-referencing, so simply use the referenced type. |
| 351 | if IsGoTypeReference(sref.Ref) { |
| 352 | var refType string |
| 353 | |
| 354 | // check if there is an x-go-type extension next to the $ref and use that over the overrided type. |
| 355 | if extension, ok := sref.Extensions[extPropGoType]; ok { |
| 356 | var ok bool |
| 357 | refType, ok = extension.(string) |
| 358 | if !ok { |
| 359 | return Schema{}, fmt.Errorf("error turning '%s: %v' into string", |
| 360 | extPropGoType, extension) |
| 361 | } |
| 362 | } else { |
| 363 | // Convert the reference path to Go type |
| 364 | var err error |
| 365 | refType, err = RefPathToGoType(sref.Ref) |
| 366 | if err != nil { |
| 367 | return Schema{}, fmt.Errorf("error turning reference (%s) into a Go type: %s", |
| 368 | sref.Ref, err) |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return Schema{ |
| 373 | GoType: refType, |
| 374 | Description: schema.Description, |
| 375 | DefineViaAlias: true, |
| 376 | SkipOptionalPointer: skipOptionalPointer, |
| 377 | OAPISchema: schema, |
| 378 | }, nil |
| 379 | } |
| 380 | |
| 381 | outSchema := Schema{ |
| 382 | Description: schema.Description, |
| 383 | OAPISchema: schema, |
no test coverage detected