Skip to content

Simplify model schema with excluded properties#3895

Merged
bajtos merged 4 commits into
masterfrom
fix/simplify-exclude-schema
Oct 11, 2019
Merged

Simplify model schema with excluded properties#3895
bajtos merged 4 commits into
masterfrom
fix/simplify-exclude-schema

Conversation

@bajtos

@bajtos bajtos commented Oct 8, 2019

Copy link
Copy Markdown
Member

In a recent discussion about using openapi-to-graphql with LoopBack apps, we found that the schema created by LoopBack 4 is difficult to interpret. Schema for creating new model instances (e.g. NewTodo) is excluding the PK property (e.g. id) using not and anyOf operators, see #3297.

IMO, we should not need to use such construct, it should be enough to omit the excluded property from the schema.

While investigating such fix, I found out that JSON Schema allows arbitrary additional properties by default (additionalProperties: true). At the same time, LoopBack 4 models are strict by default and don't allow arbitrary properties.

The first commit removes this inconsistency by modifying repository-json-schema to correctly set additionalProperties based on strict model setting.

The second commits fixes openapi-v3 to preserve boolean values of additionalProperties field. (Before my change, only objects were honored.)

The last commit finally simplifies the schema with excluded properties.

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 requested review from a team, jannyHou, nabdelgadir and raymondfeng October 8, 2019 12:02
@emonddr

emonddr commented Oct 8, 2019

Copy link
Copy Markdown
Contributor

Coverage is slightly lower than before. Please introduce additional tests. thx.

@nabdelgadir nabdelgadir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, but would it be possible to add a test that excludes more than one property?

@agnes512 agnes512 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, just one question 😄

Comment thread packages/openapi-v3/src/json-to-schema.ts

@jannyHou jannyHou left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 The code change LGTM.

@bajtos

bajtos commented Oct 8, 2019

Copy link
Copy Markdown
Member Author

Coverage is slightly lower than before. Please introduce additional tests. thx.

@emonddr good point. Unfortunately I am not able to figure out which new lines are not covered now!

The coverage report is confusing. It says that overall coverage fell down, but then it also says that coverage in examples and extensions stayed the same and went up in packages.

Screen Shot 2019-10-08 at 17 02 51

Maybe this is some kind of a rounding error, considering that the decrease is 0.0006%?

Can you perhaps spot which line is not covered?

LoopBack 4 models are strict by default, they allow only properties
explicitly described in model definition.

On the other hand, JSON Schema is open by default and allows additional
properties to be included in the payload.

This change improves our code building JSON Schema from Model definition
to correctly set `additionalProperties` flag, honoring `strict` setting.

Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
By default, additional properties are allowed in all objects.

Before this change, our code converting JSON Schema to OpenAPI Schema
was discarding `additionalProperties` field, therefere making it
impossible to describe a schema that does not allow additional props.

This change is fixing the conversion code to preserve
`additionalProperties` value.

Additionaly, we also fix the code generating schema for Filter & Where
to set `additionalProperties` and to correctly apply default value
of model setting `strict` (which is `true`).

Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
Remove the "not" section, rely on the fact that additional properties
are disabled by default and therefore it's enough to exclude the
configured properties from the schema.

Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
@bajtos bajtos force-pushed the fix/simplify-exclude-schema branch from 8a6ed1a to d46cbe8 Compare October 10, 2019 15:09
@bajtos bajtos self-assigned this Oct 10, 2019
@bajtos bajtos added feature OpenAPI Schema REST Issues related to @loopback/rest package and REST transport in general labels Oct 10, 2019
@bajtos bajtos merged commit b554ac8 into master Oct 11, 2019
@bajtos bajtos deleted the fix/simplify-exclude-schema branch October 11, 2019 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature OpenAPI REST Issues related to @loopback/rest package and REST transport in general Schema

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants