TypeSchemaWithPrefix produces the JSON schema corresponding to the given data type and adds the provided prefix to the type name
(api *expr.APIExpr, t expr.DataType, prefix string)
| 313 | // TypeSchemaWithPrefix produces the JSON schema corresponding to the given data type |
| 314 | // and adds the provided prefix to the type name |
| 315 | func TypeSchemaWithPrefix(api *expr.APIExpr, t expr.DataType, prefix string) *Schema { |
| 316 | s := NewSchema() |
| 317 | switch actual := t.(type) { |
| 318 | case expr.Primitive: |
| 319 | s.Type = Type(actual.Name()) |
| 320 | switch actual.Kind() { |
| 321 | case expr.AnyKind: |
| 322 | // A schema without a type matches any data type. |
| 323 | // See https://swagger.io/docs/specification/data-models/data-types/#any. |
| 324 | s.Type = Type("") |
| 325 | case expr.IntKind, expr.Int64Kind, |
| 326 | expr.UIntKind, expr.UInt64Kind: |
| 327 | // Use int64 format for IntKind and UIntKind because the OpenAPI |
| 328 | // generator produced int32 by default. |
| 329 | s.Type = Type("integer") |
| 330 | s.Format = "int64" |
| 331 | case expr.Int32Kind, expr.UInt32Kind: |
| 332 | s.Type = Type("integer") |
| 333 | s.Format = "int32" |
| 334 | case expr.Float32Kind: |
| 335 | s.Type = Type("number") |
| 336 | s.Format = "float" |
| 337 | case expr.Float64Kind: |
| 338 | s.Type = Type("number") |
| 339 | s.Format = "double" |
| 340 | case expr.BytesKind: |
| 341 | s.Type = Type("string") |
| 342 | s.Format = "byte" |
| 343 | } |
| 344 | case *expr.Array: |
| 345 | s.Type = Array |
| 346 | s.Items = NewSchema() |
| 347 | buildAttributeSchema(api, s.Items, actual.ElemType) |
| 348 | case *expr.Object: |
| 349 | s.Type = Object |
| 350 | for _, nat := range *actual { |
| 351 | if !MustGenerate(nat.Attribute.Meta) { |
| 352 | continue |
| 353 | } |
| 354 | prop := NewSchema() |
| 355 | buildAttributeSchema(api, prop, nat.Attribute) |
| 356 | s.Properties[nat.Name] = prop |
| 357 | } |
| 358 | case *expr.Map: |
| 359 | s.Type = Object |
| 360 | if actual.KeyType.Type == expr.String && actual.ElemType.Type != expr.Any { |
| 361 | // Use free-form objects when elements are of type "Any" |
| 362 | additionalProperties := NewSchema() |
| 363 | s.AdditionalProperties = buildAttributeSchema(api, additionalProperties, actual.ElemType) |
| 364 | } else { |
| 365 | s.AdditionalProperties = true |
| 366 | } |
| 367 | case *expr.Union: |
| 368 | // Unions are represented as an object with a discriminated value. |
| 369 | // The field names are configurable via Meta tags (defaults: "type" and "value"). |
| 370 | typeKey := actual.GetTypeKey() |
| 371 | valueKey := actual.GetValueKey() |
| 372 |
no test coverage detected