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

Implement getJsonSchemaRef and getModelSchemaRef helpers #2631

Closed
3 tasks
bajtos opened this issue Mar 22, 2019 · 0 comments · Fixed by #2971
Closed
3 tasks

Implement getJsonSchemaRef and getModelSchemaRef helpers #2631

bajtos opened this issue Mar 22, 2019 · 0 comments · Fixed by #2971
Assignees
Labels
feature OpenAPI Relations Model relations (has many, etc.) REST Issues related to @loopback/rest package and REST transport in general

Comments

@bajtos
Copy link
Member

bajtos commented Mar 22, 2019

Introduce helpers for building JSON/OpenAPI schema referencing shared model definition.

// packages/repository-json-schema/src/build-schema.ts
export function getJsonSchemaRef(
  ctor: Function,
  options: JsonSchemaOptions = {},
): JSONSchema {
  const schemaWithDefinitions = getJsonSchema(ctor, options);
  const key = schemaWithDefinitions.title;

  // ctor is not a model
  if (!key) return schemaWithDefinitions;

  const definitions = Object.assign({}, schemaWithDefinitions.definitions);
  const schema = Object.assign({}, schemaWithDefinitions);
  delete schema.definitions;
  definitions[key] = schema;

  return {
    $ref: `#/definitions/${key}`,
    definitions,
  };
}

// packages/openapi-v3/src/controller-spec.ts
export function getModelSchemaRef(
  modelCtor: Function,
  options: JsonSchemaOptions,
) {
  const jsonSchema = getJsonSchemaRef(modelCtor, options);
  return jsonToSchemaObject(jsonSchema);
}

See the spike #2592 for more details.

This story requires us to implement Allow controllers to provide definitions of models referenced in operation spec #2629 first.

Acceptance criteria

  • Implement the two new helpers described above
  • Test coverage
  • API documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature OpenAPI Relations Model relations (has many, etc.) REST Issues related to @loopback/rest package and REST transport in general
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants