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

Add a transform method for schema #144

Merged
merged 9 commits into from Jan 21, 2019
Merged

Add a transform method for schema #144

merged 9 commits into from Jan 21, 2019

Conversation

Roms1383
Copy link
Contributor

Add a transform option to convert route.schema on documentation generation in dynamic mode.

Add transform options in opts to be able to convert schema properties if required
Update documentation for transform option
Roms1383 added a commit to Roms1383/robust-database that referenced this pull request Jan 20, 2019
This version will be able to generate documentation if fastify-swagger merge the PR fastify/fastify-swagger#144 for the new transform option
@Roms1383
Copy link
Contributor Author

As a side note, I had the same errors locally as in travis-ci when running TypeScript tsc --project ./tsconfig.json the first time after install : I had to install @types/node to fix it.

Add mention about the mode and fix notes
@mcollina
Copy link
Member

I’m not sure why, but can you add that as a devDependency?

We’ll need CI to pass to land this.

@mcollina
Copy link
Member

Can you add a unit test? Also, can you update the typings?

@Roms1383
Copy link
Contributor Author

Done @mcollina, waiting for CI

@Roms1383
Copy link
Contributor Author

Ah, forgot the typings sorry...

/**
* Overwrite the route schema
* @default false
*/
Copy link
Member

Choose a reason for hiding this comment

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

I don’t think the default is false. Typings also have tests.

Copy link
Contributor Author

@Roms1383 Roms1383 Jan 20, 2019

Choose a reason for hiding this comment

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

I'm gonna fix it and currently looking at for the unit-tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Where do you usually test dynamic mode ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for being so long about the unit-tests, I'm not familiar with tap.
Also I don't know to which extend you need to test the property.

@Roms1383
Copy link
Contributor Author

Ok it should be good by now :)
I had to add the devDependencies for the unit-tests : if you think that's not ok, please provide me with feedback.

@Roms1383
Copy link
Contributor Author

As a side note I noticed that it doesn't play well with response but I don't know if it comes from Joi schema or Fastify internals. From what I remember, error got generated located in genResponse when trying to create description property.

@Roms1383
Copy link
Contributor Author

Anyway my guess is that the user who implements transform method has the responsibility to make it work and can even filter which one to transform or not so that shouldn't be blocking.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

@mcollina mcollina merged commit 20aab51 into fastify:master Jan 21, 2019
@mcollina
Copy link
Member

This seems a pretty innocent feature to have. I don't really understand your points, would you mind opening an issue or a follow up PR?

@Roms1383
Copy link
Contributor Author

@mcollina Actually after testing and looking at documentation, to make the story short that's OK 😃

In details, my test was something like :

const params = ...
const body = ...
const querystring = ...
const response = {
  200: Joi.object().keys({ message: Joi.string().required() }).required()
}

Looking at fastify documentation I understand why adding a joi object for schema's response generate an issue : as stated here the response will anyway get used with fast-json-stringify which is the bug I got 👍

not ok Converting circular structure to JSON
    found:
      name: TypeError
      stack: |-
        TypeError: Converting circular structure to JSON
            at stringify (/Users/romain/Development/sandbox/fastify-swagger/node_modules/fast-json-stable-stringify/index.js:42:19)
            at stringify (/Users/romain/Development/sandbox/fastify-swagger/node_modules/fast-json-stable-stringify/index.js:50:25)
            at stringify (/Users/romain/Development/sandbox/fastify-swagger/node_modules/fast-json-stable-stringify/index.js:50:25)
            at module.exports (/Users/romain/Development/sandbox/fastify-swagger/node_modules/fast-json-stable-stringify/index.js:58:7)
            at Ajv._addSchema (/Users/romain/Development/sandbox/fastify-swagger/node_modules/ajv/lib/ajv.js:293:30)
            at Ajv.compile (/Users/romain/Development/sandbox/fastify-swagger/node_modules/ajv/lib/ajv.js:111:24)
            at isValidSchema (/Users/romain/Development/sandbox/fastify-swagger/node_modules/fast-json-stringify/index.js:917:7)
            at build (/Users/romain/Development/sandbox/fastify-swagger/node_modules/fast-json-stringify/index.js:39:3)
            at getValidatorForStatusCodeSchema (/Users/romain/Development/sandbox/fastify-swagger/node_modules/fastify/lib/validation.js:13:10)
            at /Users/romain/Development/sandbox/fastify-swagger/node_modules/fastify/lib/validation.js:19:21
            at Array.reduce (<anonymous>)
            at getResponseSchema (/Users/romain/Development/sandbox/fastify-swagger/node_modules/fastify/lib/validation.js:18:22)
            at build (/Users/romain/Development/sandbox/fastify-swagger/node_modules/fastify/lib/validation.js:53:31)
            at afterRouteAdded (/Users/romain/Development/sandbox/fastify-swagger/node_modules/fastify/fastify.js:636:9)
            at /Users/romain/Development/sandbox/fastify-swagger/node_modules/fastify/fastify.js:596:7
            at Object._encapsulateThreeParam (/Users/romain/Development/sandbox/fastify-swagger/node_modules/avvio/boot.js:389:7)
      message: Converting circular structure to JSON
    at:
      line: 62
      column: 7
      file: test/transform.js
      function: fastify.ready.err
    stack: |
      fastify.ready.err (test/transform.js:62:7)
      Object._encapsulateThreeParam (node_modules/avvio/boot.js:380:13)
      Boot.callWithCbOrNextTick (node_modules/avvio/boot.js:311:5)
      release (node_modules/fastq/queue.js:127:16)
      Object.resume (node_modules/fastq/queue.js:61:7)
      Boot.Plugin.loadPlugin.call (node_modules/avvio/boot.js:146:20)
      toLoad.finish (node_modules/avvio/plugin.js:169:7)
      done (node_modules/avvio/plugin.js:127:5)
      check (node_modules/avvio/plugin.js:138:7)
    source: |
      t.error(err)

Last but not least it's actually mentioned here that :

(...) maybe you want to change the validation library. Perhaps you like Joi. In this case, you can use it to validate the url parameters, body, and query string!

And there's no mention of validating schema's response, so it makes sense.
Thanks !

mcollina pushed a commit that referenced this pull request Jan 21, 2019
* Add transform in opts

Add transform options in opts to be able to convert schema properties if required

* Update documentation

Update documentation for transform option

* Lint code

* Update documentation

Add mention about the mode and fix notes

* Add @types/node as a dev dependency

* Update typings

* Remove wrong default

* Add basic unit-test

* Fix unit-tests
Roms1383 added a commit to Roms1383/robust-database that referenced this pull request Feb 8, 2019
This version will be able to generate documentation if fastify-swagger merge the PR fastify/fastify-swagger#144 for the new transform option
@alexanderniebuhr alexanderniebuhr mentioned this pull request Mar 3, 2021
4 tasks
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.

None yet

2 participants