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

Fix properties removal for openapi format. #129

Closed
wants to merge 2 commits into from

Conversation

benoitbzl
Copy link

Properly detect empty objects before parsing.

Properly detect empty objects before parsing.
@kalinchernev
Copy link
Contributor

Hi @benoitbzl I tested your changes with this test:
0001-Add-test-for-CLI-removing-apis-from-definition.patch.zip

Do I understand correctly what you're trying to achieve? I'm assuming your intentions after reading one of the lately submitted issues at #128

If my assumptions are correct, could you please add the patch to your pull request and solve it? (I couldn't see the apis property being removed with the pull request)

@benoitbzl
Copy link
Author

Hi @kalinchernev My PR is not about fixing #128.
It fixes an issue we have that the entry "definitions" was getting cleared even if it was not empty. The test to check if an object is empty was not correct.
I guess that excluding some properties of the resulting specification is part of the swagger-parser package role.

@kalinchernev
Copy link
Contributor

@benoitbzl ok, that's good to know. Could you please expand the test cases with your known issues?

@benoitbzl
Copy link
Author

Sure. Sorry, I always forget about the test cases :-( I will update the commit.

@kalinchernev
Copy link
Contributor

@benoitbzl thanks!
I just upgraded all dependencies as well and published 3.0.3 which has updates in theswagger-parser.

Copy link
Contributor

@kalinchernev kalinchernev left a comment

Choose a reason for hiding this comment

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

@benoitbzl thank you for expanding on the tests!

I checked out your branch, pulled latest changes from the upstream master and all looks ok.

However, I couldn't manage to validate the newly added example reference specification. So far, all other reference specifications in examples are showing how certain documentations are parsed to validated outputs. Could you please check the feedback added below from the swagger editor?

Could you please also get latest changes from the upstream master?

To be honest, I still don't quite see which properties are removed and the reason to rewrite the helper isEmptyObject

// eslint-disable-next-line
Object.keys(obj).forEach(key => {
if (key in obj) return false;
return !Object.keys(obj).some(key => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Feel free to rename the helper to negation in order to match the new logic.
To be honest, I don't quite see the optimisation of the rewrite.

Copy link
Author

Choose a reason for hiding this comment

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

I didn't change the logic of the function. The previous one was not working as plan. It was always returning true.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't mind refactoring in it, honestly.
Rather I'm trying to see which cases haven't been covered.
Previously, there was a similar discussion, here's the background #122 (comment)
In general, the aim of this helper is to say whether an object key contains a value which is an empty object, i.e. the whole property is empty.

@@ -0,0 +1,91 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

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

I couldn't validate this spec. Using https://editor.swagger.io/ and pasting the openapi.json, which converts to

openapi: 3.0.0
info:
  version: 1.0.0
  title: Sample specification testing definitions
paths:
  /api/v1/audits:
    post:
      operationId: addAudit
      tags:
        - Audits
      description: Create a new audit
      consumes:
        - application/json
      produces:
        - application/json
      parameters:
        - in: body
          name: Audit initialization data
          description: The necessary initialization data to create an audit object
          schema:
            type: object
            properties:
              filter:
                $ref: '#/definitions/Filter'
              time:
                $ref: '#/definitions/Time'
      responses:
        '200':
          description: return the created audit
        '400':
          description: Invalid or missing parameters
        '500':
          description: Server error
definitions:
  Filter:
    description: Query filters
    type: array
    items:
      type: object
      properties:
        key:
          description: field name
          required: true
          type: string
        value:
          description: field value
          required: true
          type: {}
        op:
          description: comparaison operation
          required: false
          type: string
          enum:
            - =
  Time:
    description: Audit query time range
    type: object
    properties:
      to:
        description: time range end
        type: number
      from:
        description: time range start
        type: number
components: {}
tags: []

Seems to be invalid following the feedback:

Schema error should NOT have additional properties
additionalProperty: definitions
Jump to line 0
Schema error at paths['/api/v1/audits'].post
should NOT have additional properties
additionalProperty: consumes, produces
Jump to line 7
Schema error at paths['/api/v1/audits'].post.parameters[0].in
should be equal to one of the allowed values
allowedValues: path, query, header, cookie
Jump to line 17
Schema error at paths['/api/v1/audits'].post.parameters[0]
should match exactly one schema in oneOf
Jump to line 17

I searched to find information about the definitions property, but couldn't find any. Could it be a feature in WIP or something unsupported yet?

Copy link
Author

Choose a reason for hiding this comment

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

That's our bad. Indeed the generated openapi.json file shows error in the swagger editor. But when we render it with the latest swagger-ui component, it seemed to be correctly rendered without error...
Anyway, I will fix the example so that it is accepted by swagger editor and compliant with openapi 3.x spec.

'callback',
'links',
'petstore',
'definitions',
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice approach on expanding the test suit 👍

// Imaginary API helper
module.exports = function test(app) {
/**
* @swagger
Copy link
Contributor

Choose a reason for hiding this comment

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

If this part of the spec is not related to an endpoint/resource, I'd hoist it before the app export. I.e. in the other examples globals are before the module.exports.

@@ -55,16 +53,16 @@ function getSpecificationObject(options) {
specHelper.addDataToSwaggerObject(specification, swaggerJsDocComments);
}

if (specification.openapi) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Definitely makes sense to clean unnecessary properties before the parsing 👍

@benoitbzl
Copy link
Author

I will try to make the changes tomorrow.

@benoitbzl
Copy link
Author

Hi @kalinchernev. I fixed our documentation to be openapi 3.0 compliant and the original issue does not show up anymore ; the existing swagger-jsdoc package works perfectly. So I think we can close that PR as it was not needed originally.

@kalinchernev
Copy link
Contributor

@benoitbzl thanks for the follow up! I'm closing the pull request.
Out of curiosity, what was the tricky part? Was it the definitions part?

@benoitbzl
Copy link
Author

The 'definitions' property (among others) is not supported at the root of the swagger.json file for openapi 3. Instead, one has to put common definitions into the section components/schemas.
The use of the online swagger editor helped us to write correct documentation in that case.

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