oliyh / martian

The HTTP abstraction library for Clojure/script, supporting OpenAPI, Swagger, Schema, re-frame and more
MIT License
525 stars 42 forks source link

Martian could be more specific in the schemas #145

Closed bombaywalla closed 2 years ago

bombaywalla commented 2 years ago

For the following OpenAPI spec

openapi: 3.0.2
info:
  version: "2.0"
  title: Testing Martian
servers:
  - url: "https://example.com"
security:
  - bearer: []
paths:
  /v1/campaigns:
    get:
      operationId: listCampaigns
      summary: Gets an array of campaigns.
      parameters:
        - $ref: '#/components/parameters/startIndex'
      responses:
        200:
          description: Success.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Campaign'
        401:
          $ref: '#/components/responses/Unauthorized'
        404:
          $ref: '#/components/responses/NotFound'
components:
  parameters:
    startIndex:
      name: startIndex
      in: query
      schema:
        type: number
        default: 0
  responses:
    NotFound:
      description: The requested resource was not found.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error' 
    Unauthorized:
      description: Unauthorized.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
  schemas:
    Campaign:
      properties:
        name:
          description: The name of the campaign.
          type: string
    Error:
      properties:
        details:
          description: A human-readable description of the error.
          type: string

martian generates the following:

user> (def m (martian-http/bootstrap-openapi "http://localhost:8090/testsp.yaml" martian-http/default-opts))
#'user/m
user> (martian/explore m :list-campaigns)
{:summary "Gets an array of campaigns.",
 :parameters {{:k :start-index} (default Any 0)},
 :returns {200 [Any], 401 nil, 404 nil}}

I would have expected something closer to:

user> (martian/explore m :list-campaigns)
{:summary "Gets an array of campaigns.",
 :parameters {{:k :start-index} (default Num 0)},
 :returns {200 [{{:k :name} java.lang.String}], 401 nil, 404 nil}}

And, there seems to be something wrong with the way martian handles the schema for the error codes.

From my reading of this issue the above spec is valid.

bombaywalla commented 2 years ago

PRs #146, #147 and #148 address this issue completely. So, closing it.