MCPcopy
hub / github.com/danielgtaylor/huma / addSchemaField

Method addSchemaField

transforms.go:51–80  ·  view source on GitHub ↗
(oapi *OpenAPI, content *MediaType)

Source from the content-addressed store, hash-verified

49}
50
51func (t *SchemaLinkTransformer) addSchemaField(oapi *OpenAPI, content *MediaType) bool {
52 if content == nil || content.Schema == nil || content.Schema.Ref == "" || !strings.HasPrefix(content.Schema.Ref, "#/") {
53 return true
54 }
55
56 schema := oapi.Components.Schemas.SchemaFromRef(content.Schema.Ref)
57 if schema.Type != TypeObject || (schema.Properties != nil && schema.Properties["$schema"] != nil) {
58 return true
59 }
60
61 // Create an example so it's easier for users to find the schema URL when
62 // they are reading the documentation.
63 server := "https://example.com"
64 for _, s := range oapi.Servers {
65 if s.URL != "" {
66 server = s.URL
67 break
68 }
69 }
70
71 schema.Properties["$schema"] = &Schema{
72 Type: TypeString,
73 Format: "uri",
74 Description: "A URL to the JSON Schema for this object.",
75 ReadOnly: true,
76 Examples: []any{server + t.schemasPath + "/" + path.Base(content.Schema.Ref) + ".json"},
77 }
78
79 return false
80}
81
82// OnAddOperation is triggered whenever a new operation is added to the API,
83// enabling this transformer to precompute & cache information about the

Callers 1

OnAddOperationMethod · 0.95

Calls 1

SchemaFromRefMethod · 0.65

Tested by

no test coverage detected