up9inc / mockintosh

Mocks for microservice environments
https://mockintosh.io
MIT License
169 stars 18 forks source link

Make a dirty hack to read application/json responses of OpenAPI 3.X schemas. #170

Open apollovy opened 2 years ago

apollovy commented 2 years ago

Hey!

I've found that for OpenAPI schemas v3.X the responses don't get parsed at all, thus making mockintosh almost unusable. I've made up this quick-and-dirty hack and things got better. I don't expect this PR to be merged as is, because, obviously, it will break backwards compatibility with version 2.X aka Swagger. Also the only content-type that is handled now is application/json, while OpenAPI 3.X may specify multiple content-types and thus multiple different schemas for responses. Hope someone will be able to make things better.

Cheers.

undera commented 2 years ago

Can you provide some more context? Some input example that you use to reproduce the use-case

apollovy commented 2 years ago

Yeah, ok, sorry, made the PR deep in the evening, forgotten best practices.

Most simple spec I can get is here:

openapi: 3.0.2
info:
  title: Playground
  version: 1.0.0
servers:
  - url: 'http://localhost:8000'
paths:
  '/foo':
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Foo'
          description: Gets a list of all `Foo` entities.
components:
  schemas:
    Foo:
      type: object
      properties:
        bar:
          type: number
        baz:
          type: string

Then I run unstable or latest:

docker run --rm -it -p 8000-8005:8000-8005 -v ~/Downloads/playground.openapi.yaml:/playground.openapi.yaml up9inc/mockintosh:unstable /playground.openapi.yaml

and then test:

curl -v localhost:8001/foo 
*   Trying ::1:8001...
* Connected to localhost (::1) port 8001 (#0)
> GET /foo HTTP/1.1
> Host: localhost:8001
> User-Agent: curl/7.77.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Sat, 02 Apr 2022 09:39:42 GMT
< Server: Mockintosh/0.13.17
< X-Mockintosh-Prompt: Hello, I'm Mockintosh.
< Etag: "da39a3ee5e6b4b0d3255bfef95601890afd80709"
< Content-Length: 0
<
* Connection #0 to host localhost left intact

As you see, the content-length is 0.

Then I try running the patched version:

docker run --rm -it -p 8000-8005:8000-8005 -v ~/Downloads/playground.openapi.yaml:/playground.openapi.yaml apollov/mockintosh:0.13.17.apollov.1 /playground.openapi.yaml

and get:

curl -v localhost:8001/foo
*   Trying ::1:8001...
* Connected to localhost (::1) port 8001 (#0)
> GET /foo HTTP/1.1
> Host: localhost:8001
> User-Agent: curl/7.77.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Sat, 02 Apr 2022 09:43:06 GMT
< Server: Mockintosh/0.13.17
< X-Mockintosh-Prompt: Hello, I'm Mockintosh.
< Content-Type: application/json
< Etag: "0224568838dffacfe1feb14452922648684b97f8"
< Content-Length: 100
<
* Connection #0 to host localhost left intact
{"bar": 4.3848451753112033e+307, "baz": "Because change them some money police beat support along."}