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 gov.nist.secauto.metaschema.core.model.IDefinition;
11  
12  import edu.umd.cs.findbugs.annotations.NonNull;
13  
14  /**
15   * Supports generation of a JSON schema based on a Metaschema definition, which
16   * can be generated inline or as a JSON schema definition.
17   */
18  public interface IJsonSchemaDefinition extends IJsonSchemaDefinable {
19    @Override
20    default boolean isInline(IJsonGenerationState state) {
21      return state.isInline(getDefinition());
22    }
23  
24    /**
25     * Get the associated definition.
26     *
27     * @return the definition
28     */
29    @NonNull
30    IDefinition getDefinition();
31  
32    @Override
33    default void generateDefinitionJsonSchema(ObjectNode node, IJsonGenerationState state) {
34      node.put("$id", JsonSchemaHelper.generateDefinitionJsonPointer(this));
35  
36      JsonSchemaHelper.generateTitle(getDefinition(), node);
37      JsonSchemaHelper.generateDescription(getDefinition(), node);
38      generateBody(node, state);
39    }
40  
41    @Override
42    default void generateInlineJsonSchema(ObjectNode node, IJsonGenerationState state) {
43      // do not generate the metadata, since this will be the responsibility of the
44      // property
45      generateBody(node, state);
46    }
47  
48    /**
49     * Generate the body of the JSON schema.
50     *
51     * @param node
52     *          the JSON node to generate the schema within
53     * @param state
54     *          the generation state used to generate this JSON schema
55     */
56    void generateBody(@NonNull ObjectNode node, @NonNull IJsonGenerationState state);
57  }