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

Update OpenAPI (SQL COMMENT to description, constraints, cleaning up) #885

Merged
merged 24 commits into from Jul 22, 2017
Merged

Update OpenAPI (SQL COMMENT to description, constraints, cleaning up) #885

merged 24 commits into from Jul 22, 2017

Conversation

ldesgoui
Copy link
Contributor

@ldesgoui ldesgoui commented Jun 3, 2017

Greetings,

This PR addresses the idea raised in #222.
I launched the PR to ask feedback: what do you feel deserves change in the current OpenAPI generation ?

To do before completion:

Most of this seems trivial and should be accomplished soon :)

Side note:
I've updated the ToJSON instances but I don't think they're used anywhere, maybe they could get removed? (EDIT: removed now)
What about fluffing up the hard-coded OpenAPI options a little more while I'm at it ? They're not the most welcoming in the world and could definitely redirect developers to postgrest.com documentation

Links:
Postgres SQL Comments
PostgraphQL's introspection query

@begriffs
Copy link
Member

begriffs commented Jun 8, 2017

You know I'm really not an OpenAPI expert. I think wherever you want the comments to appear in the output is probably a great start. Perhaps other people can chime in here. Just doing the hard part of obtaining those comments and putting them somewhere is wonderful, and it will be easier to tweak later on as people have suggestions as they use it. Having some repetition seems OK. The output can be pretty huge anyway, and I'd rather have API consumers notice the comments than overlook them.

I do like the idea of allowing a schema comment to override the generic API description placeholder. Also regarding your side note, feel free to try removing unused instances and cleaning up the code. My philosophy is that if the existing tests keep passing then we should be good.

Thanks for jumping in and working on this! I know people have wanted it for a while. Once you add some tests and a changelog entry we can look into getting this merged.

Anybody with opinions please add a comment.

@ldesgoui ldesgoui changed the title [WIP] Use Postgres' COMMENT feature to document OpenAPI [WIP] Update OpenAPI (SQL COMMENT to description, constraints, cleaning up) Jun 8, 2017
@begriffs
Copy link
Member

I learned from previous work that when the openapi test times out it is actually failing because of malformed openapi output. Not sure why it does't just return an error. In the past I copied the output into an online validator to get a useful error message.

@begriffs
Copy link
Member

begriffs commented Jul 1, 2017

I know the test suite isn't very helpful when it fails for OpenAPI validation. Let's see if we can work together to figure out the problem. I checked out your branch, built it, and ran postgrest against the database schema created by our test suite. I was able to get more information about the validation failure by copying the openapi output into http://bigstickcarpet.com/swagger-parser/www/index.html :

Swagger schema validation failed. 
  Too few properties defined (0), minimum 1 at #/paths//withUnique/delete/responses
 
JSON_OBJECT_VALIDATION_FAILED

Error: Swagger schema validation failed. 
  Too few properties defined (0), minimum 1 at #/paths//withUnique/delete/responses
 
JSON_OBJECT_VALIDATION_FAILED
    at http://bigstickcarpet.com/swagger-parser/www/js/bundle.min.js:11:318
    at http://bigstickcarpet.com/swagger-parser/www/js/bundle.min.js:50:316
    at <anonymous> 

SyntaxError: Swagger schema validation failed. 
  Too few properties defined (0), minimum 1 at #/paths//withUnique/delete/responses
 
JSON_OBJECT_VALIDATION_FAILED
    at Function.syntax (http://bigstickcarpet.com/swagger-parser/dist/swagger-parser.min.js:284:318)
    at validateSchema (http://bigstickcarpet.com/swagger-parser/dist/swagger-parser.min.js:20:254)
    at http://bigstickcarpet.com/swagger-parser/dist/swagger-parser.min.js:8:1921
    at <anonymous> 

z-schema validation error: JSON_OBJECT_VALIDATION_FAILED
    at ZSchema.getLastError (http://bigstickcarpet.com/swagger-parser/dist/swagger-parser.min.js:2047:3479)
    at validateSchema (http://bigstickcarpet.com/swagger-parser/dist/swagger-parser.min.js:20:158)
    at http://bigstickcarpet.com/swagger-parser/dist/swagger-parser.min.js:8:1921
    at <anonymous>

Does this error help?

@ldesgoui
Copy link
Contributor Author

ldesgoui commented Jul 2, 2017

Oh, that seems easy to fix, deleteOp in OpenAPI.makePathItem doesn't contain a "at" field
Thanks a lot for the help. I'll be continuing work on the PR this week, unless you'd rather discuss #790 further.

@begriffs
Copy link
Member

begriffs commented Jul 2, 2017

That discussion would affect another major version anyway, and we still may keep the openapi description but use content negotiation (the Accept request header) to show it rather than show it by default. Also extracting sql comments into our in-memory data structure will be useful no matter how we output the api description so carry on!

@ldesgoui
Copy link
Contributor Author

ldesgoui commented Jul 12, 2017

I tried using the tool you linked but I had no chance with it, when I click "Validate it!", a pretty-print of the description shows up in a box that shakes for a second and nothing else happens/shows up, where did you get those error messages?
EDIT: other validators seem to have no problem with the output, sad that the test doesn't let us know more 😦

On another note, I've been slowly reading the OpenAPI spec, it seems most of the repetition can be abstracted away using global definitions. I'll probably make the output less "precise" but smaller by removing the enums/patterns generated with columns/operators

Also: I was interested in using Vendor Extensions for FK/PK/constraints but they're not supported by hackage's swagger2 :(

@ldesgoui
Copy link
Contributor Author

How the output looks after the removal of all duplication: https://gist.github.com/ldesgoui/faecf9fec35e150f67be1cb7843c1f7e

@ldesgoui ldesgoui closed this Jul 17, 2017
@ldesgoui
Copy link
Contributor Author

Misclick, sorry 😦

@ldesgoui ldesgoui reopened this Jul 17, 2017
@begriffs
Copy link
Member

The new output looks really nice, I can actually read it directly rather than the old avalanche of text. Great work!

Did you manage to figure out the validator I was using? Looks like the tests are failing on the most recent commit except this time they are giving a real error message rather than just hanging.

@ldesgoui
Copy link
Contributor Author

I did, yes, the output is valid now. The tests fail because they need to be updated to reflect changes

@ldesgoui ldesgoui changed the title [WIP] Update OpenAPI (SQL COMMENT to description, constraints, cleaning up) Update OpenAPI (SQL COMMENT to description, constraints, cleaning up) Jul 19, 2017
@ldesgoui
Copy link
Contributor Author

Hey, I'm done with this PR, here's a recap:

  • Dead ToJSON intances were remvoed
  • ProcDescription, Table, Column received a new Maybe Text field for descriptions
  • Introspection received an additional SQL query to get the Schema COMMENT
  • RPC body schema was inlined as it's only used once
  • All Table path properties were moved to Parameter Definitions as they're all used more than once
  • OpenAPI links to PostgREST docs

colDescription c
s =
(mempty :: Schema)
& default_ .~ (decode . TL.encodeUtf8 . TL.fromStrict =<< colDefault c)
Copy link
Member

@begriffs begriffs Jul 19, 2017

Choose a reason for hiding this comment

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

Is it possible to use the more generic toS to convert the string types? Something like

default_ .~ (decode . toS =<< colDefault c)

Not sure if it can infer the types, but thought I'd ask.

@begriffs
Copy link
Member

Thanks for all your work to research this and finish it!

@begriffs begriffs merged commit 6d5f72b into PostgREST:master Jul 22, 2017
@ldesgoui
Copy link
Contributor Author

My pleasure, postgrest is pure joy to use in projects :)

"type": "string"
},
"parent_id": {
"description": "Note:\nThis is a Foreign Key to `entities.id`.<fk table='entities' column='id'/>",
Copy link
Member

Choose a reason for hiding this comment

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

@ldesgoui I've always wondered.. Where does the <fk> and <pk> tags come from?

@ldesgoui
Copy link
Contributor Author

ldesgoui commented Aug 27, 2020 via email

@steve-chavez
Copy link
Member

@ldesgoui I see, thanks for the quick reply!

According to OAI/OpenAPI-Specification#587, another way of specifying the pk/fk would be with OpenAPI extensions.
This could be a future enhancement.

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

Successfully merging this pull request may close these issues.

None yet

3 participants