Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve includes to allow code splitting #204

Open
AlexGustafsson opened this issue Jul 2, 2020 · 0 comments
Open

Resolve includes to allow code splitting #204

AlexGustafsson opened this issue Jul 2, 2020 · 0 comments

Comments

@AlexGustafsson
Copy link

AlexGustafsson commented Jul 2, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants