MCPcopy Index your code
hub / github.com/oapi-codegen/oapi-codegen / GenerateGoSchema

Function GenerateGoSchema

pkg/codegen/schema.go:326–657  ·  view source on GitHub ↗
(sref *openapi3.SchemaRef, path []string)

Source from the content-addressed store, hash-verified

324}
325
326func 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,

Callers 12

oapiSchemaToGoTypeFunction · 0.85
paramToGoTypeFunction · 0.85
generateUnionFunction · 0.85
mergeSchemasV1Function · 0.85
GenStructFromAllOfFunction · 0.85
mergeSchemasFunction · 0.85
GenerateTypesForSchemasFunction · 0.85
GenerateBodyDefinitionsFunction · 0.85

Calls 15

combinedSchemaExtensionsFunction · 0.85
IsGoTypeReferenceFunction · 0.85
RefPathToGoTypeFunction · 0.85
extTypeNameFunction · 0.85
MergeSchemasFunction · 0.85
PathToTypeNameFunction · 0.85
additionalPropertiesTypeFunction · 0.85
SortedSchemaKeysFunction · 0.85
generateUnionFunction · 0.85

Tested by

no test coverage detected