Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.io.File;
import java.nio.file.Path;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -42,6 +43,8 @@ public class OpenAPITask extends BaseTask {

private List<File> adoc;

private Boolean mcp;

/**
* Creates an OpenAPI task.
*/
Expand Down Expand Up @@ -89,7 +92,11 @@ public void generate() throws Throwable {
OpenAPI result = tool.generate(mainClass);

var adocPath = ofNullable(adoc).orElse(List.of()).stream().map(File::toPath).toList();
for (var format : OpenAPIGenerator.Format.values()) {
var formats = EnumSet.allOf(OpenAPIGenerator.Format.class);
if (mcp != Boolean.TRUE) {
formats.remove(OpenAPIGenerator.Format.MCP);
}
for (var format : formats) {
tool.export(result, format, Map.of("adoc", adocPath))
.forEach(output -> getLogger().info(" writing: " + output));
}
Expand Down Expand Up @@ -154,6 +161,21 @@ public void setIncludes(@Nullable String includes) {
this.includes = includes;
}

/**
* Beta generate a mcp like file format. Disabled by default.
*
* @return Mcp.
*/
@Input
@org.gradle.api.tasks.Optional
public Boolean getMcp() {
return mcp;
}

public void setMcp(Boolean mcp) {
this.mcp = mcp;
}

/**
* Regular expression used to excludes route. Example: <code>/web</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -49,6 +50,9 @@ public class OpenAPIMojo extends BaseMojo {
@Parameter(property = "openAPI.specVersion")
private String specVersion;

@Parameter(property = "openAPI.mcp")
private Boolean mcp;

@Parameter private List<File> adoc;

@Override
Expand Down Expand Up @@ -80,6 +84,10 @@ protected void doExecute(@NonNull List<MavenProject> projects, @NonNull String m
var result = tool.generate(mainClass);

var adocPath = ofNullable(adoc).orElse(List.of()).stream().map(File::toPath).toList();
var formats = EnumSet.allOf(OpenAPIGenerator.Format.class);
if (mcp != Boolean.TRUE) {
formats.remove(OpenAPIGenerator.Format.MCP);
}
for (var format : OpenAPIGenerator.Format.values()) {
tool.export(result, format, Map.of("adoc", adocPath))
.forEach(output -> getLog().info(" writing: " + output));
Expand Down Expand Up @@ -144,4 +152,12 @@ public List<File> getAdoc() {
public void setAdoc(List<File> adoc) {
this.adoc = adoc;
}

public Boolean getMcp() {
return mcp;
}

public void setMcp(Boolean mcp) {
this.mcp = mcp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal.openapi;

import static io.swagger.v3.oas.models.Components.COMPONENTS_SCHEMAS_REF;

import java.util.NoSuchElementException;
import java.util.Optional;

import io.swagger.v3.oas.models.media.Schema;

public class OpenApiSupport {

protected final OpenAPIExt openapi;

public OpenApiSupport(OpenAPIExt openapi) {
this.openapi = openapi;
}

public Schema<?> resolveSchema(Schema<?> schema) {
if (schema.get$ref() != null) {
return resolveSchemaInternal(schema.get$ref())
.orElseThrow(() -> new NoSuchElementException("Schema not found: " + schema.get$ref()));
}
return schema;
}

protected Optional<Schema<?>> resolveSchemaInternal(String name) {
var components = openapi.getComponents();
if (components == null || components.getSchemas() == null) {
throw new NoSuchElementException("No schema found");
}
if (name.startsWith(COMPONENTS_SCHEMAS_REF)) {
name = name.substring(COMPONENTS_SCHEMAS_REF.length());
}
return Optional.ofNullable((Schema<?>) components.getSchemas().get(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
package io.jooby.internal.openapi.asciidoc;

import static io.swagger.v3.oas.models.Components.COMPONENTS_SCHEMAS_REF;
import static java.util.Optional.ofNullable;

import java.io.IOException;
Expand All @@ -29,6 +28,7 @@
import io.jooby.SneakyThrows;
import io.jooby.StatusCode;
import io.jooby.internal.openapi.OpenAPIExt;
import io.jooby.internal.openapi.OpenApiSupport;
import io.pebbletemplates.pebble.PebbleEngine;
import io.pebbletemplates.pebble.error.PebbleException;
import io.pebbletemplates.pebble.extension.AbstractExtension;
Expand All @@ -45,7 +45,7 @@
import io.swagger.v3.oas.models.media.NumberSchema;
import io.swagger.v3.oas.models.media.Schema;

public class AsciiDocContext {
public class AsciiDocContext extends OpenApiSupport {
public static final BiConsumer<String, Schema<?>> NOOP = (name, schema) -> {};

private ObjectMapper json;
Expand All @@ -70,6 +70,7 @@ public class AsciiDocContext {
}

public AsciiDocContext(Path baseDir, ObjectMapper json, ObjectMapper yaml, OpenAPIExt openapi) {
super(openapi);
this.json = json;
this.yamlOpenApi = yaml;
this.yamlOutput = newYamlOutput();
Expand Down Expand Up @@ -304,14 +305,6 @@ public String schemaType(Schema<?> schema) {
return Optional.ofNullable(resolved.getFormat()).orElse(resolved.getType());
}

public Schema<?> resolveSchema(Schema<?> schema) {
if (schema.get$ref() != null) {
return resolveSchemaInternal(schema.get$ref())
.orElseThrow(() -> new NoSuchElementException("Schema not found: " + schema.get$ref()));
}
return schema;
}

public Object schemaProperties(Schema<?> schema) {
var resolved = resolveSchema(schema);
if ("array".equals(resolved.getType())) {
Expand Down Expand Up @@ -464,17 +457,6 @@ public Schema<?> resolveSchema(String path) {
return schema;
}

private Optional<Schema<?>> resolveSchemaInternal(String name) {
var components = openapi.getComponents();
if (components == null || components.getSchemas() == null) {
throw new NoSuchElementException("No schema found");
}
if (name.startsWith(COMPONENTS_SCHEMAS_REF)) {
name = name.substring(COMPONENTS_SCHEMAS_REF.length());
}
return Optional.ofNullable((Schema<?>) components.getSchemas().get(name));
}

public PebbleEngine getEngine() {
return engine;
}
Expand Down
Loading