1 /*
2 * SPDX-FileCopyrightText: none
3 * SPDX-License-Identifier: CC0-1.0
4 */
5
6 package gov.nist.secauto.metaschema.schemagen.json.impl;
7
8 import com.fasterxml.jackson.databind.node.ObjectNode;
9
10 import java.util.Set;
11 import java.util.stream.Stream;
12
13 import edu.umd.cs.findbugs.annotations.NonNull;
14
15 /**
16 * Supports generation of a JSON schema, which can be generated inline or as a
17 * JSON schema definition.
18 * <p>
19 * Inline JSON schema generation is supported using the
20 * {@link #generateInlineJsonSchema(ObjectNode, IJsonGenerationState)} method.
21 * <p>
22 * JSON schema definition generation is supported using the
23 * {@link #generateDefinitionJsonSchema(ObjectNode, IJsonGenerationState)} and a
24 * definition reference can be generated using
25 * {@link #generateDefinitionReference(ObjectNode, IJsonGenerationState)}.
26 * <p>
27 * The
28 * {@link #generateJsonSchemaOrDefinitionRef(ObjectNode, IJsonGenerationState)}
29 * method can be used to ensure that an inline schema or reference is generated
30 * based on the inline behavior (see {@link #isInline(IJsonGenerationState)}.
31 */
32 public interface IJsonSchemaDefinable extends IJsonSchema {
33
34 /**
35 * Used to recursively collect definitions that are used within the model graph
36 * for descendants of this node.
37 *
38 * @param visited
39 * the ancestor assembly definitions that have been visited
40 * @param state
41 * the generation state used to generate this JSON schema
42 * @return a stream of JSON schema definition object referenced within
43 * descendants within this node's graph
44 */
45 @NonNull
46 Stream<IJsonSchemaDefinable> collectDefinitions(
47 @NonNull Set<IJsonSchemaDefinitionAssembly> visited,
48 @NonNull IJsonGenerationState state);
49
50 @Override
51 default void generateJsonSchemaOrDefinitionRef(ObjectNode node, IJsonGenerationState state) {
52 if (isInline(state)) {
53 generateInlineJsonSchema(node, state);
54 } else {
55 generateDefinitionReference(node, state);
56 }
57 }
58
59 /**
60 * The JSON schema definition name that will be used by definition references.
61 *
62 * @param state
63 * the generation state used to generate this JSON schema
64 * @return the name, without the definition path
65 * @see #generateDefinitionReference(ObjectNode, IJsonGenerationState)
66 */
67 @NonNull
68 String getDefinitionName();
69
70 /**
71 * Generate a JSON schema definition reference for the JSON schema definition
72 * representing the Metaschema-based model object associated with this object.
73 *
74 * @param node
75 * the JSON node to generate the reference within
76 * @param state
77 * the generation state used to generate this JSON schema
78 */
79 default void generateDefinitionReference(@NonNull ObjectNode node, @NonNull IJsonGenerationState state) {
80 node.put("$ref", JsonSchemaHelper.generateDefinitionJsonPointer(this));
81 }
82
83 /**
84 * Generate a JSON schema representing the Metaschema-based model object
85 * associated with this object.
86 *
87 * @param node
88 * the JSON node to generate the schema within
89 * @param state
90 * the generation state used to generate this JSON schema
91 */
92 void generateDefinitionJsonSchema(@NonNull ObjectNode node, @NonNull IJsonGenerationState state);
93 }