Class: ReeSwagger::BuildEndpointSchema

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_swagger/package/ree_swagger/functions/build_endpoint_schema.rb

Constant Summary collapse

METHODS_WITH_BODY =
[:post, :put, :patch].freeze
MissingCasterError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#call(endpoint) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ree_lib/packages/ree_swagger/package/ree_swagger/functions/build_endpoint_schema.rb', line 19

def call(endpoint)
  path_params = []

  path = endpoint.path
    .split('/')
    .map {
      if _1.start_with?(':')
        path_param = _1[1..-1]
        path_params << path_param.to_sym
        "{#{path_param}}"
      else
        _1
      end
    }
    .join('/')

  mime_type = get_mime_type(endpoint.respond_to)

  missed_caster = path_params - endpoint.caster&.fields&.keys.to_a

  if missed_caster.any?
    raise MissingCasterError, "missing caster for path parameters #{missed_caster.inspect}"
  end

  if endpoint.caster && METHODS_WITH_BODY.include?(endpoint.method)
    parameters = build_parameters(endpoint.caster, path_params, false)

    request_body_schema = build_request_body_schema(
      endpoint.caster,
      path_params
    )

    request_body = request_body_schema && {
      content: {
        :"#{mime_type}" => {
          schema: request_body_schema
        }
      }
    }
  elsif endpoint.caster
    parameters = build_parameters(endpoint.caster, path_params, true)
  end

  request_body =
    if endpoint.caster && METHODS_WITH_BODY.include?(endpoint.method)
      request_body_schema = build_request_body_schema(
        endpoint.caster,
        path_params
      )

      request_body_schema && {
        content: {
          :"#{mime_type}" => {
            schema: request_body_schema
          }
        }
      }
    end

  response_schema = {
    description: endpoint.response_description || ''
  }

  if endpoint.serializer
    response_schema[:content] = {
      :"#{mime_type}" => {
        schema: build_serializer_schema(endpoint.serializer)
      }
    }
  end

  responses = {
    endpoint.response_status => response_schema
  }

  endpoint.errors.each do |error|
    if responses.key?(error.status)
      responses[error.status][:description] += "\n- #{error.description}"
      next
    end

    responses[error.status] = {
      description: "- #{error.description}",
    }
  end

  method_schema = {
    responses: responses
  }

  method_schema[:summary] =  endpoint.summary if endpoint.summary
  method_schema[:description] =  endpoint.description if endpoint.description
  method_schema[:tags] = endpoint.sections if endpoint.sections

  method_schema[:parameters]  = parameters   if parameters
  method_schema[:requestBody] = request_body if request_body
  method_schema[:security] = [{ ApiKeyAuth: [] }] if endpoint.authenticate

  schema = {endpoint.method => method_schema}

  PathDto.new(
    path: path,
    schema: schema
  )
end