Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

apollovy
Copy link

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
Copy link
Contributor

undera commented Apr 2, 2022

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

@apollovy
Copy link
Author

apollovy commented Apr 2, 2022

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."}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants