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

Custom schema title for POST requests #3716

Merged
merged 2 commits into from
Sep 16, 2019
Merged

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Sep 12, 2019

While reviewing #3698, #3504 and #1466, it occurred to me that schema titles like TodoExcluding[id] or ProductPartialExcluding[id,_rev] are not very friendly to human consumers.

In this pull request, I am introducing a new JsonSchemaOption title that allows developer to specify a title that described the intent (data of a new Todo to be created - NewTodo) instead of the implementation details (Todo data excluding id property).

The second commit updates controller templates and rest-crud package to leverage this new schema option for POST requests.

/cc @ericzon @remkohdev

IMPORTANT: Although this change is very likely to fix the issues described in #3698, #3504 and #1466 for newly created controllers, it's not meant as a replacement for #3504. I'd like #3504 to be landed, because it solves the problems for existing applications and users that decide to not use the title option for whatever reasons.

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

@bajtos bajtos self-assigned this Sep 12, 2019
@bajtos
Copy link
Member Author

bajtos commented Sep 12, 2019

Discussion point:

  • The auto-generated titles add a suffix to the model name, e.g. TodoPartial, TodoWithRelations.
  • In this PR, I am using a prefix for the schema describing payload of a new model instance, e.g. NewTodo, because NewTodo looks better to me than TodoNew.

This introduces a small inconsistency in schema titles. Do we mind?

Please:

  • Upvote (👍) this comment if you agree with NewTodo.
  • Downvote (👎 ) this comment if you prefer TodoNew for consistency.

/cc @strongloop/loopback-next

@hacksparrow
Copy link
Contributor

NewTodo should be made the new consistency standard, if it is not already.

Where is TodoNew used? I couldn't find any instance.

@bajtos
Copy link
Member Author

bajtos commented Sep 12, 2019

Thank you @hacksparrow for the comment.

NewTodo should be made the new consistency standard, if it is not already.

Where is TodoNew used? I couldn't find any instance.

TodoNew is not used anywhere yet, but we are using TodoPartial and TodoWithRelations for PATCH and GET operations. (The titles TodoPartial and TodoWithRelations are auto-generated, you can view them when you run e.g. examples/todo and open the API Explorer.)

I see two options now:

  • NewTodo for POST, but TodoPartial for PATCH - this is the current implementation in my PR
  • TodoNew for POST and TodoPartial for PATCH - this is the other option, one that's more consistent IMO

@hacksparrow
Copy link
Contributor

I don't feel any inconsistency in NewTodo vs. TodoPartial and TodoWithRelations.

I understand TodoPartial as (ExistingTodo)Partial, and TodoWithRelations as (ExistingTodo)WithRelations.

Copy link
Contributor

@agnes512 agnes512 left a comment

Choose a reason for hiding this comment

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

Mostly look good to me, just a few comments.

}

const schema = getJsonSchema(TestModel, {
title: 'NewTestModel',
Copy link
Contributor

@agnes512 agnes512 Sep 12, 2019

Choose a reason for hiding this comment

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

IIUC, the default format for the schema title is New+ModelName. And if we don't specified the title, it would be NewTestModel in this case.
The title of this test is uses custom title when provided by user. It replaces the title from IgnoredTitle to 'custom title' NewTestModel. But IgnoredTitle looks more like a custom title to me in this case.
Is it possible to change it to make the intention more clear?

Copy link
Member Author

@bajtos bajtos Sep 12, 2019

Choose a reason for hiding this comment

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

@agnes512 Good point!

Let me clarify: The default format for the schema title would be IgnoredTitleExcluding['id'].

Model-level title setting IgnoredTitle applies to all calls of getJsonSchema where no options.title is provided. In my use case, I want to provide a different title in each getJsonSchema call.

Maybe I should rework this test into two? One verifying that model-level title is ignored, another verifying that suffix computed from options is ignored too?

What else can I do to make the intention more clear? If it's enough to improve the test name, could you please suggest a better one?

Thanks! 🙇

Copy link
Contributor

@jannyHou jannyHou Sep 12, 2019

Choose a reason for hiding this comment

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

Good discussion here 👏

Some personal suggestion: maybe split it into two test cases like:

  • title in decorator > title inferred from model class name: verify the title in @model() overrides the title inferred from model class name. no options involved in this test.
  • title in options > title in decorator: verify the title in options overrides the title in @model(). And obviously option also overrides the one inferred from the model class(proved by the 1st test)

As a summary:
title in options > title in decorator > title inferred from model class name

Copy link
Contributor

Choose a reason for hiding this comment

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

Janny's suggestion looks good to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 3d3e79b.

While improving the tests, I modified my new tests to call modelToJsonSchema instead of getJsonSchema, because they are not relying on caching behavior of getJsonSchema.

@dhmlau
Copy link
Member

dhmlau commented Sep 12, 2019

@bajtos, for consistency's sake, I think it might be better to have TodoNew rather than NewTodo. But I don't have a strong preference on that. Both options are better than the current title. :)

@nabdelgadir
Copy link
Contributor

@bajtos so just to confirm, if the user chooses to use this title option, then the schema options will be available through the description after #3504 is landed?

And I agree with @hacksparrow's point; it feels as though the beginning part is describing whether it's New or Existing and the following part is the description of model schema options.

@bajtos
Copy link
Member Author

bajtos commented Sep 12, 2019

So just to confirm, if the user chooses to use this title option, then the schema options will be available through the description after #3504 is landed?

Yes, that's my intention 👍

And I agree with @hacksparrow's point; it feels as though the beginning part is describing whether it's New or Existing and the following part is the description of model schema options.

That's a great way how to explain the schema naming convention 👍

Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

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

The change in PR looks reasonable to me, I left my understanding and suggestion in https://github.com/strongloop/loopback-next/pull/3716/files#r323856015, I feel it would be good to document how title from different places take precedence over each other :)

@bajtos
Copy link
Member Author

bajtos commented Sep 13, 2019

I have realized that my original implementation had a flaw in the way how the schema is cached. I added 84db706 to fix it.

@jannyHou @agnes512 @nabdelgadir LGTY now?

Copy link
Contributor

@agnes512 agnes512 left a comment

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@nabdelgadir nabdelgadir left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

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

👏 cool

Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
…st bodies

Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
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

6 participants