Loopback-next: x-ts-type requires model pointed in #/components/schemas

Created on 21 Oct 2018  路  11Comments  路  Source: strongloop/loopback-next

Description / Steps to reproduce / Feature proposal

To reproduce, you may checkout develop branch of this repo first,
https://github.com/chunghha/lb-poc/commit/5aa2da90281484b581a64dbea0872d21d8fc406f
then uncomment the line of 'x-ts-type': Country in the country.controller.ts

Current Behavior

Once the above steps done, open http://explorer.loopback.io/?url=http://localhost:3000/openapi.json#/CountryController/get_country
to see the error, "Could not resolve reference because of: Could not resolve pointer: /components/schemas/Country does not exist in document"

Expected Behavior

Country model (country.model.ts) exists in src/models so expect that model pointed with 'x-ts-type' without re-writing properties of Country to #/components/schemas.

TOB bug

All 11 comments

It seems like your generated code is a bit out-of-date.
I'm trying to recreate the app based on your commit provided above, and there is "$ref": "#/components/schemas/Country" in the openapi.json

You can see my version of the app here: https://github.com/dhmlau/temp-country-poc:

"get": {
        "x-controller-name": "CountryController",
        "x-operation-name": "find",
        "tags": [
          "CountryController"
        ],
        "responses": {
          "200": {
            "description": "Array of Country model instances",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Country"
                  }
                }
              }
            }
          }
        },

Hope it helps.

Thanks a lot, @dhmlau for taking a look. I made a commit to my develop branch, but still don't get it. I mean I see

"items": {
                    "$ref": "#/components/schemas/Country"
                  }

if I uncomment the x-ts-type line from the controller but I don't see #/components/schemas/Country with properties generated into openapi.json.

I see you extended DefaultCrudRepository and find data from it. In my case, I am supposed to get data from the service provider. Does this make a difference on automatically generating #/component/schemas/Country to openapi.json? I feel I am missing something here.

@chunghha, sorry that I missed that you've commented out the x-ts-type in the controller. Yes, it does make a difference if you commented it, because in that case, there is no way to know what will get returned when generating the openapi.json.

The questions to ask would be:
Does the service provider return the same model (data structure) as yours?
What do you want your API to return?

When the x-ts-type line is uncommented, I don't see #/components/schemas/Country generated, only $ref line is generated so answers to your questions.

  1. The service provider returns more than what I want. I like to control response properties defined in my country.model.ts. When I need to add more properties I will add them to the Country model.

  2. My country service's "countries" service endpoint will return response as [Country].

In the end, what I would like to test is that country model generated into openapi.json will be schema to oasgraph query. I am able to do same with Apollo server (my gql-poc develop branch) regarding control of schema model but one step further to generate GraphQL schema from openapi.json automatically via oasgraph is what I am looking for.

Here is relevant debug output of my problem.

loopback:openapi3:metadata:controller-spec   operation for method getCountries: {"verb":"get","path":"/countries","spec":{"responses":{"200":{"description":"Array of Country model instance","content":{"application/json":{"schema":{"type":"array","items":{}}}}}}}} +1ms
  loopback:openapi3:metadata:controller-spec   spec responses for method getCountries: { '200': { description: 'Array ofCountry model instance', content: { 'application/json': [Object] } } } +0ms
  loopback:openapi3:metadata:controller-spec   evaluating response code 200 with content: 'application/json' +0ms
  loopback:openapi3:metadata:controller-spec   x-ts-type => undefined +0ms
  loopback:openapi3:metadata:controller-spec   processing parameters for method getCountries +0ms

So code in these two don't generate #/components/schemas/Country with properties from the Country model into openapi.json dynamically (at least for my code).
https://github.com/strongloop/loopback-next/blob/bf329719db08cacc011aaaf88bf29aad6bed391d/packages/openapi-v3/src/controller-spec.ts#L113-L136

https://github.com/strongloop/loopback-next/blob/bf329719db08cacc011aaaf88bf29aad6bed391d/packages/openapi-v3/src/generate-schema.ts#L17-L42

Hi, I have a similar problem, if I add an x-ts-type to a response schema in an operation decorator only the $ref gets generated, but not the relative component schema

I'm also experiencing this. The entire #/components is not being generated in the resulting /openapi.json Is there a workaround/known issue for this?

-- edit --

Perhaps this should be better documented. I was able to get the components defined by doing the following:

// YourModel.model.ts
class YourModel extends Entity {
  ...
}
export const YourModelSchema = getJsonSchema(YourModel)

// YourController.controller.ts
@api({
  paths: {},
  components: {
    schemas: {
      YourModel: YourModelSchema,
    },
  },
})
class Controller {
  ...
}

I see the problem now. A fix is on the way soon.

I need to add more tests to the PR before it can be merged.

Thanks @raymondfeng, I see the commit landed. I will confirm this issue with next release.

I am glad I can confirm that openapi-v3 1.0.2 fixed this issue.

Was this page helpful?
0 / 5 - 0 ratings