module OpenAPI::Generator::Serializable

Overview

The Serializable module automatically generates an OpenAPI Operations representation of the class or struct when extended.

Example

struct Model
  extend OpenAPI::Generator::Serializable
  include JSON::Serializable

  property string : String
  property opt_string : String?
  @[OpenAPI::Field(ignore: true)]
  property ignored : Nil
  @[OpenAPI::Field(type: String, example: "1")]
  @cast : Int32

  def cast
    @cast.to_s
  end
end

puts Model.to_openapi_schema.to_pretty_json
# => {
#   "required": [
#     "string",
#     "cast"
#   ],
#   "type": "object",
#   "properties": {
#     "string": {
#       "type": "string"
#     },
#     "opt_string": {
#       "type": "string"
#     },
#     "cast": {
#       "type": "string",
#       "example": "1"
#     }
#   }
# }

Usage

Extending this module adds a self.to_openapi_schema that returns an OpenAPI representation inferred from the shape of the class or struct.

The class name is also registered as a global component schema and will be available for referencing from any Controller annotation from a reference object.

See: OpenAPI::Generator::Controller::Schema.ref

NOTE Calling #to_openapi_schema programatically is unnecessary. The Generator will take care of serialization while producing the openapi yaml file.

Defined in:

openapi-generator/serializable.cr

Constant Summary

SERIALIZABLE_CLASSES = [] of Class

A list of all serializable subclasses.

Instance Method Summary

Macro Summary

Instance Method Detail

def generate_schema #

Serialize the class into an OpenAPI::Schema representation.

Check the swagger documentation for more details


def to_openapi_schema #

Serialize the class into an OpenAPI::Reference representation.

Check the swagger documentation for more details


Macro Detail

macro generate_schema(schema, types, as_type = nil, read_only = false, write_only = false, schema_key = nil, example = nil) #