-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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(rest): assign all component properties to target spec #2757
Conversation
86ad4ef
to
f21f1be
Compare
f21f1be
to
ef3d285
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nabdelgadir The request body specification should be added in the operation spec, instead of under the components
field.
See the doc for OpenAPI 3 operation object.
Usually, we add referenced schema in components
, and the request body spec refers to it.
A good example you can look at is the OpenAPI spec for a pet store app, it's an official example:
Spec to define the request body
Spec to define schema in components
@jannyHou thanks, Janny! The reason I was copying it to I'm not too familiar with LoopBack 3, but I don't believe the spec includes |
@nabdelgadir IIRC LoopBack 3 is using swagger 2.x spec to describe the REST API, which means you may need to convert it to OpenAPI 3.x spec then add the spec to route. If the spec is already converted or already on OpenAPI 3.0, the missing part could be generating OpenAPI schema for LB3 models and add it to |
@jannyHou They're already added to The problem is that the explorer is looking for |
@nabdelgadir I see thank you for posting the spec here.
This looks suspicious to me. I think the correct place for searching should be |
ef3d285
to
9cf9473
Compare
I believe
In general, endpoints can use different schemas for the request body. For example, I am going to use the example app from #2803 to review the generated schema and better understand what's going on. |
Note that entries in {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CoffeeShop"
}
},
"application/x-www-form-urlencoded": {
"schema": {
"$ref": "#/components/schemas/CoffeeShop"
}
},
"application/xml": {
"schema": {
"$ref": "#/components/schemas/CoffeeShop"
}
},
"text/xml": {
"schema": {
"$ref": "#/components/schemas/CoffeeShop"
}
}
},
"description": "Model instance data"
} |
I am proposing to fix the problem by changing diff --git a/packages/rest/src/router/router-spec.ts b/packages/rest/src/router/router-spec.ts
index 31b8aa2e..32f71b94 100644
--- a/packages/rest/src/router/router-spec.ts
+++ b/packages/rest/src/router/router-spec.ts
@@ -8,10 +8,13 @@ import {OpenApiSpec} from '@loopback/openapi-v3-types';
export type RouterSpec = Pick<OpenApiSpec, 'paths' | 'components' | 'tags'>;
export function assignRouterSpec(target: RouterSpec, additions: RouterSpec) {
- if (additions.components && additions.components.schemas) {
+ if (additions.components) {
if (!target.components) target.components = {};
- if (!target.components.schemas) target.components.schemas = {};
- Object.assign(target.components.schemas, additions.components.schemas);
+
+ for (const key in additions.components) {
+ if (!target.components[key]) target.components[key] = {};
+ Object.assign(target.components[key], additions.components[key]);
+ }
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make sure we all understand the problem in full depth and then find a robust solution. See my comments above to get the discussion started.
@bajtos thanks for chiming in! In my first commit, I transferred over I don't know if you introduced |
Well, I suppose we can work incrementally and add
I think it was me who introduced Now that I have a better understanding, it makes most sense to me to copy (assign) all |
@bajtos thanks, I agree. I'll update it to assign all of |
9cf9473
to
56978a1
Compare
I am not sure either. Since the drop is |
@bajtos Thank you for looking into the issue. I thought the explorer is looking for a missing schema, didn't realize we do generate reusable
+1 I am not aware of any fields in https://github.com/OAI/OpenAPI-Specification/blob/3.0.1/versions/3.0.1.md#componentsObject shouldn't be copied in a LB4 app, so I am good with the patch @bajtos posted above. I am going to review the code details asap. @nabdelgadir you can click here to check why the coverage decreases: In you case I think it's some code in the hello word example causes the drop, see https://coveralls.io/jobs/48100920/source_files/566824703 And feel free to ignore, "Since the drop is -0.002%, I think it's ok to ignore it." +1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Came from #2803 to see what this PR was about, learned some stuff :-).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 LGTM good job!
56978a1
to
ddfd078
Compare
ddfd078
to
f68b54b
Compare
When using
mountExpressRouter
to mount a LoopBack 3 application, the explorer complains when trying to do aPOST
request (for LB3 models), e.g.:And this doesn't allow a
POST
(or similar) request to be made through the explorer:By adding all of
component
's properties,components.requestBodies
will be added to the spec and this allows requests to be made through the explorer:Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated👉 Check out how to submit a PR 👈