Module: Modern::DocGenerator::OpenAPI3::Operations

Included in:
Paths
Defined in:
lib/modern/doc_generator/open_api3/operations.rb

Instance Method Summary collapse

Instance Method Details

#_operation(route) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/modern/doc_generator/open_api3/operations.rb', line 7

def _operation(route)
  {
    operationId: route.id,
    summary: route.summary,
    description: route.description,
    deprecated: route.deprecated,
    tags: route.tags.uniq,

    security: route.security.map { |s| _security_requirement(s) },

    parameters: route.parameters.map { |p| _parameter(p) },
    requestBody: _request_body(route),
    responses:
      route.responses_by_code.map do |code, response|
        [code, _response(response)]
      end.to_h,
    # TODO: implement callbacks
    callbacks: nil
  }.compact
end

#_parameter(parameter) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/modern/doc_generator/open_api3/operations.rb', line 36

def _parameter(parameter)
  # TODO: this should be in the parameter class, or that logic should be here.

  parameter.to_openapi3.merge(
    schema: _build_schema_value({}, {}, parameter.type)
  )
end

#_request_body(route) ⇒ Object



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
# File 'lib/modern/doc_generator/open_api3/operations.rb', line 44

def _request_body(route)
  body = route.request_body

  if body.nil?
    nil
  else
    schema =
      if body.type.nil?
        nil
      elsif body.type.is_a?(Class) && body.type.ancestors.include?(Dry::Struct)
        _struct_ref(body.type)
      else
        # TODO: make this less wasteful (see _response)
        _build_schema_value({}, {}, body.type)
      end

    {
      required: body.required,
      content:
        route.input_converters.map(&:media_type).map do |content_type|
          [
            content_type,
            {
              schema: schema
            }.compact
          ]
        end.to_h.compact
    }
  end
end

#_response(response) ⇒ Object



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
# File 'lib/modern/doc_generator/open_api3/operations.rb', line 75

def _response(response)
  {
    description: response.description,
    # headers:
    #   response.headers.map do |header|
    #     [
    #       header.name,
    #       {
    #         description: header.description
    #       }.compact
    #     ]
    #   end.to_h,
    content:
      response.content_by_type.map do |content_type, content|
        [
          content_type,
          {
            schema:
              if content.type.nil?
                nil
              elsif content.type.is_a?(Class) && content.type.ancestors.include?(Dry::Struct)
                _struct_ref(content.type)
              else
                # TODO: make this less wasteful
                #       Right now, this reuses the schema walking stuff. It totally will re-walk
                #       existing schemas as far as `content.type` will reach. Which is slower
                #       during startup than we'd like, but it's fixable later by converting
                #       the methods to using state (that's why `OpenAPI3` is an object in the
                #       first place, but I wrote myself into a corner here).
                _build_schema_value({}, {}, content.type)
              end
          }.compact
        ]
      end.to_h
  }.compact
end

#_security_requirement(security) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/modern/doc_generator/open_api3/operations.rb', line 28

def _security_requirement(security)
  # TODO: OAuth2
  ret = {}
  ret[security.name] = []

  ret
end