1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.schemagen.json.impl;
7   
8   import com.fasterxml.jackson.databind.node.ObjectNode;
9   
10  import dev.metaschema.core.model.IDefinition;
11  import edu.umd.cs.findbugs.annotations.NonNull;
12  
13  /**
14   * Supports generation of a JSON schema based on a Metaschema definition, which
15   * can be generated inline or as a JSON schema definition.
16   */
17  public interface IJsonSchemaDefinition extends IJsonSchemaDefinable {
18    @Override
19    default boolean isInline(IJsonGenerationState state) {
20      return state.isInline(getDefinition());
21    }
22  
23    /**
24     * Get the associated definition.
25     *
26     * @return the definition
27     */
28    @NonNull
29    IDefinition getDefinition();
30  
31    @Override
32    default void generateDefinitionJsonSchema(ObjectNode node, IJsonGenerationState state) {
33      node.put("$id", JsonSchemaHelper.generateDefinitionJsonPointer(this));
34  
35      JsonSchemaHelper.generateTitle(getDefinition(), node);
36      JsonSchemaHelper.generateDescription(getDefinition(), node);
37      generateBody(node, state);
38    }
39  
40    @Override
41    default void generateInlineJsonSchema(ObjectNode node, IJsonGenerationState state) {
42      // do not generate the metadata, since this will be the responsibility of the
43      // property
44      generateBody(node, state);
45    }
46  
47    /**
48     * Generate the body of the JSON schema.
49     *
50     * @param node
51     *          the JSON node to generate the schema within
52     * @param state
53     *          the generation state used to generate this JSON schema
54     */
55    void generateBody(@NonNull ObjectNode node, @NonNull IJsonGenerationState state);
56  }