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

feat: support $ref #1208

Merged
merged 6 commits into from Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/AnnotationsReader/BasicAnnotationsReader.ts
Expand Up @@ -5,14 +5,15 @@ import { Annotations } from "../Type/AnnotatedType";
import { symbolAtNode } from "../Utils/symbolAtNode";

export class BasicAnnotationsReader implements AnnotationsReader {
private static requiresDollar = new Set<string>(["id", "comment"]);
private static requiresDollar = new Set<string>(["id", "comment", "ref"]);
private static textTags = new Set<string>([
"title",
"description",
"id",

"format",
"pattern",
"ref",

// New since draft-07:
"comment",
Expand Down Expand Up @@ -56,7 +57,7 @@ export class BasicAnnotationsReader implements AnnotationsReader {
"deprecated",
]);

public constructor(private extraTags?: Set<string>) {}
public constructor(private extraTags?: Set<string>) { }
hmil marked this conversation as resolved.
Show resolved Hide resolved

public getAnnotations(node: ts.Node): Annotations | undefined {
const symbol = symbolAtNode(node);
Expand Down
12 changes: 9 additions & 3 deletions src/Utils/removeUnreachable.ts
Expand Up @@ -2,6 +2,8 @@ import { JSONSchema7Definition } from "json-schema";
import { Definition } from "../Schema/Definition";
import { StringMap } from "./StringMap";

const DEFINITION_OFFSET = "#/definitions/".length;

function addReachable(
definition: Definition | JSONSchema7Definition,
definitions: StringMap<Definition>,
Expand All @@ -12,9 +14,9 @@ function addReachable(
}

if (definition.$ref) {
const typeName = decodeURIComponent(definition.$ref.slice(14));
if (reachable.has(typeName)) {
// we've already processed this definition
const typeName = decodeURIComponent(definition.$ref.slice(DEFINITION_OFFSET));
if (reachable.has(typeName) || !isLocalRef(definition.$ref)) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this is too late. At this point we already generated the schema. It would be better to not even recurse into types when we have a ref.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I'll look into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated AnnotatedNodeParser such that it short-circuits child type parsing if an explicit ref annotation was provided.
Instead, it sets the child type to AnyType. The AnyType is conveniently represented as an empty object which means we'll only get the $ref property in the output schema and no additional noise coming from the type itself.

// we've already processed this definition, or this definition refers to an external schema
return;
}
reachable.add(typeName);
Expand Down Expand Up @@ -79,3 +81,7 @@ export function removeUnreachable(

return out;
}

function isLocalRef(ref: string) {
return ref.charAt(0) === "#";
}
2 changes: 2 additions & 0 deletions test/valid-data-annotations.test.ts
Expand Up @@ -39,5 +39,7 @@ describe("valid-data-annotations", () => {

it("annotation-readOnly", assertValidSchema("annotation-readOnly", "MyObject", "basic"));

it("annotation-ref", assertValidSchema("annotation-ref", "MyObject", "extended"));

it("annotation-writeOnly", assertValidSchema("annotation-writeOnly", "MyObject", "basic"));
});
12 changes: 12 additions & 0 deletions test/valid-data/annotation-ref/main.ts
@@ -0,0 +1,12 @@

export interface MyObject {
/**
* @ref http://json-schema.org/draft-07/schema#
*/
nested: MyNestedObject
}

export interface MyNestedObject {
foo: string;
bar: number;
}
18 changes: 18 additions & 0 deletions test/valid-data/annotation-ref/schema.json
@@ -0,0 +1,18 @@
{
"$ref": "#/definitions/MyObject",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyObject": {
"additionalProperties": false,
"properties": {
"nested": {
"$ref": "http://json-schema.org/draft-07/schema#"
}
},
"required": [
"nested"
],
"type": "object"
}
}
}