Class SourceGenerator

java.lang.Object
io.micronaut.jsonschema.generator.SourceGenerator

@Internal public final class SourceGenerator extends Object
A source generator to create source files from Json Schema.
Since:
1.3
Author:
Elif Kurtay
  • Constructor Details

    • SourceGenerator

      public SourceGenerator(String lang)
      Constructs a new SourceGenerator instance based on the provided programming language.

      This constructor attempts to find a corresponding SourceGenerator implementation for the given programming language. If no such implementation is found, a RuntimeException is thrown.

      Parameters:
      lang - The String representing the target programming language for which the source generator is to be created. This argument cannot be null.
      Throws:
      RuntimeException - if no matching source generator is found for the provided language. The exception message will indicate the language for which no generator was found.
    • SourceGenerator

      public SourceGenerator(io.micronaut.inject.visitor.VisitorContext.Language language, GeneratorContext context)
      Constructs a new SourceGenerator instance based on the provided programming language and the generation context.

      This constructor attempts to find a corresponding SourceGenerator implementation for the given programming language. If no such implementation is found, a RuntimeException is thrown.

      Parameters:
      language - The VisitorContext.Language representing the target programming language for which the source generator is to be created. This argument cannot be null.
      context - The GeneratorContext representing the already existing context of generation.
      Throws:
      RuntimeException - if no matching source generator is found for the provided language. The exception message will indicate the language for which no generator was found.
  • Method Details

    • generate

      public File generate(SourceGeneratorConfig config) throws IOException
      Generates source code from JSON schema files based on the provided configuration.

      This method first checks if an inputFolder is specified in the configuration. If the inputFolder is provided, it processes all JSON Schema in the folder to generate source code. If inputFolder is null, it attempts to retrieve the JSON schema from the specified inputStream, jsonUrl, or jsonFile in the configuration and generates code from the schema.

      If the outputFileName exists, the method generates a single source file from the schema with that file name. Otherwise, the method generates all objects defined in the schema inside the specified outputPath (and outputPackageName if available).

      Parameters:
      config - The SourceGeneratorConfig object that contains the configuration for source code generation, including input folder, JSON schema URL, output path, output package name, and output file name.
      Returns:
      The top level schema's generated File when a single input is given, null otherwise.
      Throws:
      IOException - If an I/O error occurs during file or directory creation, or if an error occurs while reading or writing files.
    • buildEnum

      public io.micronaut.sourcegen.model.EnumDef buildEnum(Schema jsonSchema, String builderClassName)
    • getInputFileName

      public static String getInputFileName()
    • setInputFileName

      public static void setInputFileName(String inputFileName)
    • getOutputPath

      public static Path getOutputPath()
    • getOutputPackageName

      public static String getOutputPackageName()
    • getLanguage

      public static io.micronaut.inject.visitor.VisitorContext.Language getLanguage()