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

Fix repository-json-schema to handle circular references #2628

Closed
3 tasks
bajtos opened this issue Mar 22, 2019 · 1 comment · Fixed by #2690
Closed
3 tasks

Fix repository-json-schema to handle circular references #2628

bajtos opened this issue Mar 22, 2019 · 1 comment · Fixed by #2690
Assignees
Labels
bug OpenAPI Relations Model relations (has many, etc.)

Comments

@bajtos
Copy link
Member

bajtos commented Mar 22, 2019

Description / Steps to reproduce / Feature proposal

Consider the following test case:

it('handles circular references', () => {
  @model()
  class Category {
    @property.array(() => Product)
    products?: Product[];
  }

  @model()
  class Product {
    @property(() => Category)
    category?: Category;
  }

  const schema = modelToJsonSchema(Category);
  expect(schema).to.deepEqual({
    title: 'Category',
    properties: {
      products: {
        type: 'array',
        items: {$ref: '#/definitions/Product'},
      },
    },
    definitions: {
      Product: {
        title: 'Product',
        properties: {
          category: {
            $ref: '#/definitions/Category',
          },
        },
      },
    },
  });
});

Please note this test case is synthetic and does not represent the actual design we will use for relation navigational properties.

Current Behavior

The test fails with a stack-overflow error.

       handles circular references:
     RangeError: Maximum call stack size exceeded
      at RegExp.test (<anonymous>)
      at isTypeResolver (/Users/bajtos/src/loopback/next/packages/repository/src/type-resolver.ts:44:16)
      at Object.resolveType (/Users/bajtos/src/loopback/next/packages/repository/src/type-resolver.ts:88:10)
      at metaToJsonProperty (src/build-schema.ts:148:24)
      at modelToJsonSchema (src/build-schema.ts:241:28)
      at getJsonSchema (src/build-schema.ts:38:23)
      at modelToJsonSchema (src/build-schema.ts:263:24)
      at getJsonSchema (src/build-schema.ts:38:23)
      at modelToJsonSchema (src/build-schema.ts:263:24)
      at getJsonSchema (src/build-schema.ts:38:23)
      at ` (src/build-schema.ts:263:24)
      ...

Expected Behavior

The tests passes.

Additional information

See the spike #2592 and the commit 2256435 for more details.

The idea is to introduce a new options argument to getJsonSchema and modelToJsonSchema functions, this argument will expect the following properties to begin with:

export interface JsonSchemaOptions {
  visited?: {[key: string]: JSONSchema};
}

Acceptance criteria

  • Make the changes necessary to make the above test pass
  • Add additional test coverage at unit-test and integration level as necessary
  • Update API documentation
@jannyHou
Copy link
Contributor

Closed as PR #2690 get merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug OpenAPI Relations Model relations (has many, etc.)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants