MCPcopy Create free account
hub / github.com/cli/cli / generate

Method generate

script/ghmock/restscaffold.go:225–297  ·  view source on GitHub ↗

generate makes a placeholder value for a schema. Objects get only their required properties. A required property with no schema is null: GitHub's description requires archived_at on pull request labels without defining it, and GitHub sends null.

(ref *openapi3.SchemaRef, name string, depth int)

Source from the content-addressed store, hash-verified

223// description requires archived_at on pull request labels without defining
224// it, and GitHub sends null.
225func (f *restFiller) generate(ref *openapi3.SchemaRef, name string, depth int) any {
226 if ref == nil || ref.Value == nil || depth > maxSchemaDepth {
227 return nil
228 }
229 s := ref.Value
230 if depth > 0 && allowsNull(s) {
231 return nil
232 }
233 if len(s.Enum) > 0 {
234 values := make([]string, 0, len(s.Enum))
235 for _, e := range s.Enum {
236 if str, ok := e.(string); ok {
237 values = append(values, str)
238 }
239 }
240 if len(values) == len(s.Enum) {
241 return enumPlaceholder(values)
242 }
243 return s.Enum[0]
244 }
245 if len(s.AllOf) > 0 {
246 var v any = map[string]any{}
247 for _, sub := range s.AllOf {
248 v = f.fill(sub, v, name, depth+1)
249 }
250 if isObjectSchema(s) {
251 v = f.fill(&openapi3.SchemaRef{Value: withoutAllOf(s)}, v, name, depth+1)
252 }
253 return v
254 }
255 if alts := alternatives(s); len(alts) > 0 {
256 for _, alt := range alts {
257 if alt.Value == nil || alt.Value.Type.Is("null") {
258 continue
259 }
260 v := f.generate(alt, name, depth+1)
261 if obj, ok := v.(map[string]any); ok && s.Discriminator != nil {
262 obj[s.Discriminator.PropertyName] = discriminatorValue(s.Discriminator, alt)
263 }
264 return v
265 }
266 return nil
267 }
268
269 switch {
270 case isObjectSchema(s):
271 obj := map[string]any{}
272 for _, key := range s.Required {
273 obj[key] = f.generate(s.Properties[key], key, depth+1)
274 }
275 return obj
276 case s.Type.Is("array"):
277 list := []any{}
278 for range s.MinItems {
279 list = append(list, f.generate(s.Items, name, depth+1))
280 }
281 return list
282 case s.Type.Is("string"):

Callers 2

scaffoldMethod · 0.95
fillMethod · 0.95

Calls 8

fillMethod · 0.95
allowsNullFunction · 0.85
enumPlaceholderFunction · 0.85
isObjectSchemaFunction · 0.85
withoutAllOfFunction · 0.85
alternativesFunction · 0.85
discriminatorValueFunction · 0.85
stringPlaceholderFunction · 0.85

Tested by

no test coverage detected