Class OpenApiClientGenerator

java.lang.Object
com.shri.restinpeace.codegen.OpenApiClientGenerator

public final class OpenApiClientGenerator extends Object
Generates a @RestClient interface (annotations and all) from an OpenAPI 3.x document - the opposite direction of this library's own compile-time proxy generation, which turns a hand-written interface into an implementation. Spec-first instead of annotation-first: point this at an existing API's OpenAPI JSON and get a ready-to-compile interface instead of transcribing every path/parameter by hand.

Scope: this is a skeleton generator, not a full schema-to-POJO code generator like swagger-codegen/OpenAPI Generator - every parameter and every request/response body is generated as String. OpenAPI's own {name} path-template placeholder syntax already matches @PathParam's exactly, so paths need no translation at all. What this generator gets right - the paths, HTTP methods, parameter names/locations, and base URL - is the tedious, error-prone part of a large API; narrowing a specific parameter's type or replacing a body's String with your own POJO is a normal hand-edit of the generated file afterward, not something this generator attempts.

Only get/post/put/delete/patch operations are read from each path item (head/options/ trace are skipped); a requestBody of any shape becomes one @Body String parameter; every parameters entry with in: path or in: query becomes a @PathParam/@QueryParam of type String (any other in - header/cookie - is skipped, since honoring it is a per-call/interceptor concern, not part of the interface shape). servers[0].url, if present, becomes the interface's @BaseUrl; its absence isn't an error - the generated interface then simply requires every method's own URL to already be absolute, exactly like a hand-written one would.

  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    generate(File specFile, File outputDirectory, String packageName, String interfaceName)
    Reads specFile as an OpenAPI 3.x JSON document and writes a generated @RestClient interface to <outputDirectory>/<interfaceName>.java, creating outputDirectory (and any missing parent directories) if needed.
    static void
    main(String[] args)
    Command-line entry point: specFile outputDirectory packageName interfaceName.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • generate

      public static void generate(File specFile, File outputDirectory, String packageName, String interfaceName) throws IOException
      Reads specFile as an OpenAPI 3.x JSON document and writes a generated @RestClient interface to <outputDirectory>/<interfaceName>.java, creating outputDirectory (and any missing parent directories) if needed.
      Parameters:
      specFile - the OpenAPI 3.x document, as JSON (not YAML - convert first if your spec is YAML)
      outputDirectory - the directory the generated .java file is written into
      packageName - the generated interface's package
      interfaceName - the generated interface's simple name
      Throws:
      IOException - if specFile can't be read or the output file can't be written
    • main

      public static void main(String[] args) throws IOException
      Command-line entry point: specFile outputDirectory packageName interfaceName.
      Parameters:
      args - the four positional arguments described above
      Throws:
      IOException - if specFile can't be read or the output file can't be written