gradle-swagger-generator-plugin icon indicating copy to clipboard operation
gradle-swagger-generator-plugin copied to clipboard

Resolve includes to allow code splitting

Open AlexGustafsson opened this issue 5 years ago • 0 comments

Describe the feature

By implementing support for $include: "<relative path>", one would allow for powerful code splitting in the vain of swagger-cli.

PoC:

In GenerateSwaggerUI.groovy, one could write a simple tree explorer which recursively resolves $include with the actual JSON contents of the specified file.

private JsonNode resolve(JsonNode root, File inputFile) {
    def keys = root.keys()
    while (keys.hasNext()) {
        def key = keys.next()
        def value = root.get(key)

        if (value != null && value.isObject()) {
            root.set(key, resolve(value, inputFile))
        } else if (value instanceof TextNode && key == "\$include") {
            include = new File(inputFile.getParentDirectory(), value.textValue())
            root.set(key, resolve(Mappers.YAML.readTree(include), include))
        }
    }

    return root
}

private void buildSwaggerSpec() {
    def inputJson = resolve(Mappers.YAML.readTree(inputFile), inputFile)
    new File(outputDir, 'swagger-spec.js').withWriter('UTF-8') { writer ->
        writer.append('window.swaggerSpec=')
        Mappers.JSON.writeValue(writer, inputJson)
    }
}

Example yml:

swagger: "2.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
host: petstore.swagger.io
basePath: /v1
schemes:
  - http
consumes:
  - application/json
produces:
  - application/json
paths:
  $include: ./paths.yaml
definitions:
  $include: ./models.yaml

Why do you want the feature?

Code splitting would become a breeze.

AlexGustafsson avatar Jul 02 '20 09:07 AlexGustafsson