Skip to content
New issue

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

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

Already on GitHub? # to your account

Teach Prism to stop polluting the console with AJV "unknown format" warnings #1889

Merged
merged 1 commit into from
Sep 14, 2021

Conversation

nulltoken
Copy link
Contributor

Summary

In ajv-validator/ajv#466, the author of AJV makes it very clear that people should be aware of unknown formats.

However, according to the OpenAPI spec, format is is an open string-valued property, and can have any value.

Given some traffic, all those warnings tend to pollute the console output a lot and make it very hard to follow up with what's logged by Prism.

This change intercepts AJV calls to console.warn and when they relate to unknown formats, silence them.

Checklist

  • The basics
    • I tested these changes manually in my local or dev environment
  • Tests
    • Added or updated
    • N/A
  • Event Tracking
    • I added event tracking and followed the event tracking guidelines
    • N/A
  • Error Reporting
    • I reported errors and followed the error reporting guidelines
    • N/A

Screenshots

Given the following spec openapi.yaml stored at the root of the repo workdir

openapi: 3.1.0
paths:
  /something:
    get:
      summary: Gimme me something, anything!
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  this:
                    type: string
                    format: something
servers:
- url: "https://example.com"

From a term, run the following

$ yarn cli mock ../../openapi.yaml 
yarn run v1.22.5
$ node -r ts-node/register/transpile-only -r tsconfig-paths/register src/index.ts mock ../../openapi.yaml
$ node -r ts-node/register/transpile-only -r tsconfig-paths/register src/index.ts mock ../../openapi.yaml
[12:25:34 PM] » [CLI] ...  awaiting  Starting Prism…
[12:25:34 PM] » [CLI] i  info      GET        http://127.0.0.1:4010/something
[12:25:34 PM] » [CLI] ►  start     Prism is listening on http://127.0.0.1:4010

From another term, run the following

$ curl http://127.0.0.1:4010/something
{"this":"string"}

Switch back to intial term and observe the generated log

$ node -r ts-node/register/transpile-only -r tsconfig-paths/register src/index.ts mock ../../openapi.yaml
[12:25:34 PM] » [CLI] ...  awaiting  Starting Prism…
[12:25:34 PM] » [CLI] i  info      GET        http://127.0.0.1:4010/something
[12:25:34 PM] » [CLI] ►  start     Prism is listening on http://127.0.0.1:4010
[12:25:43 PM] » [HTTP SERVER] get /something i  info      Request received
[12:25:43 PM] »     [NEGOTIATOR] i  info      Request contains an accept header: */*
[12:25:43 PM] »     [VALIDATOR] √  success   The request passed the validation rules. Looking for the best response
[12:25:43 PM] »     [NEGOTIATOR] √  success   Found a compatible content for */*
[12:25:43 PM] »     [NEGOTIATOR] √  success   Responding with the requested status code 200
unknown format "something" ignored in schema at path "#/properties/this"
unknown format "something" ignored in schema at path "#/properties/this"

Additional context

A similar issue has been opened ages ago against Spectral (stoplightio/spectral#396).

This PR shamelessly stoles is greatly inspired by @chris-miaskowski 's fix from stoplightio/spectral#503

@nulltoken nulltoken requested review from a team and Amjcraft and removed request for a team September 14, 2021 10:29
…nings

In ajv-validator/ajv#466, the author of AJV makes it very clear that people should be aware of unknown formats.

However, according to the OpenAPI spec, format is is an open string-valued property, and can have any value.

Given some traffic, all those warnings tend to pollute the console output a lot and make it very hard to follow up with what's logged by Prism.

This change intercepts AJV calls to console.warn and when they relate to unknown formats, silence them.
@nulltoken nulltoken force-pushed the ntk/unknownFormatSilencer branch from 516648f to 9c92f8e Compare September 14, 2021 12:09
Copy link
Contributor

@radzserg radzserg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@radzserg radzserg merged commit 1c62191 into stoplightio:master Sep 14, 2021
@nulltoken nulltoken deleted the ntk/unknownFormatSilencer branch September 14, 2021 12:15
@nulltoken
Copy link
Contributor Author

@radzserg Wow! That WAS fast. Thx!

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

Successfully merging this pull request may close these issues.

2 participants