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

Fixed spec validation failure when no content-type present for request #179

Closed wkok closed 1 year ago

wkok commented 1 year ago

Ref: Originating issue

When spec instrumentation is enabled, and bootstrapping a martian using an OpenAPI v3 definition containing no content-type declaration for the request (for example a GET), then spec validation fails for example:

Definition with no content-type declaration for the request:

openapi: 3.0.0
info:
  title: OpenAI API
  description: APIs for sampling from and fine-tuning language models
  version: '1.3.0'
servers:
  - url: https://api.openai.com/v1
paths:
  /models:
    get:
      operationId: listModels
      summary: Lists the currently available models
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema: {}

In a test namespace, enable spec instrumentation:

(ns wkok.openai-clojure.api-test
  (:require [clojure.spec.test.alpha :as stest]))

(stest/instrument `martian.core/build-instance)

(martian/bootstrap-openapi ... )   ;; bootstrap using above yaml

results in validation error:

1. Caused by clojure.lang.ExceptionInfo
   Spec assertion failed.

         Spec: #object[clojure.spec.alpha$regex_spec_impl$reify__2510 0x663073a3 "clojure.spec.alpha$regex_spec_impl$reify__2510@663073a3"]
        Value: ("https://api.openai.com/v1"
                ({:description nil,
                  :method :get,

...

     Problems: 

            val: [nil]
             in: [1 0 :consumes]
         failed: nil?
           spec: :martian.spec/content-types
             at: [:handlers :consumes :clojure.spec.alpha/nil]

            val: nil
             in: [1 0 :consumes 0]
         failed: string?
           spec: :martian.spec/content-types
             at: [:handlers :consumes :clojure.spec.alpha/pred]

This PR fixes it by ensuring nil for :consumes in this case instead of [nil]

oliyh commented 1 year ago

Thanks @wkok it looks good to me. It's deployed on Clojars in 0.1.24-SNAPSHOT, do you want to give it a whirl?

Cheers

wkok commented 1 year ago

Tested, works beautifully :+1:

oliyh commented 1 year ago

Nice, thanks for letting me know - I'll do a release tomorrow.