Skip to content

Commit d4bda12

Browse files
committed
WIP: openapi to mcp
- still not sure if this is useful, but it is an starts (probably doesn't make any sense later, replaced with real java code) - ref #3830
1 parent e0094cf commit d4bda12

File tree

11 files changed

+867
-37
lines changed

11 files changed

+867
-37
lines changed

modules/jooby-gradle-plugin/src/main/java/io/jooby/gradle/OpenAPITask.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.File;
1818
import java.nio.file.Path;
19+
import java.util.EnumSet;
1920
import java.util.List;
2021
import java.util.Map;
2122
import java.util.Optional;
@@ -42,6 +43,8 @@ public class OpenAPITask extends BaseTask {
4243

4344
private List<File> adoc;
4445

46+
private Boolean mcp;
47+
4548
/**
4649
* Creates an OpenAPI task.
4750
*/
@@ -89,7 +92,11 @@ public void generate() throws Throwable {
8992
OpenAPI result = tool.generate(mainClass);
9093

9194
var adocPath = ofNullable(adoc).orElse(List.of()).stream().map(File::toPath).toList();
92-
for (var format : OpenAPIGenerator.Format.values()) {
95+
var formats = EnumSet.allOf(OpenAPIGenerator.Format.class);
96+
if (mcp != Boolean.TRUE) {
97+
formats.remove(OpenAPIGenerator.Format.MCP);
98+
}
99+
for (var format : formats) {
93100
tool.export(result, format, Map.of("adoc", adocPath))
94101
.forEach(output -> getLogger().info(" writing: " + output));
95102
}
@@ -154,6 +161,21 @@ public void setIncludes(@Nullable String includes) {
154161
this.includes = includes;
155162
}
156163

164+
/**
165+
* Beta generate a mcp like file format. Disabled by default.
166+
*
167+
* @return Mcp.
168+
*/
169+
@Input
170+
@org.gradle.api.tasks.Optional
171+
public Boolean getMcp() {
172+
return mcp;
173+
}
174+
175+
public void setMcp(Boolean mcp) {
176+
this.mcp = mcp;
177+
}
178+
157179
/**
158180
* Regular expression used to excludes route. Example: <code>/web</code>.
159181
*

modules/jooby-maven-plugin/src/main/java/io/jooby/maven/OpenAPIMojo.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.io.File;
1313
import java.nio.file.Path;
1414
import java.nio.file.Paths;
15+
import java.util.EnumSet;
1516
import java.util.List;
1617
import java.util.Map;
1718
import java.util.Optional;
@@ -49,6 +50,9 @@ public class OpenAPIMojo extends BaseMojo {
4950
@Parameter(property = "openAPI.specVersion")
5051
private String specVersion;
5152

53+
@Parameter(property = "openAPI.mcp")
54+
private Boolean mcp;
55+
5256
@Parameter private List<File> adoc;
5357

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

8286
var adocPath = ofNullable(adoc).orElse(List.of()).stream().map(File::toPath).toList();
87+
var formats = EnumSet.allOf(OpenAPIGenerator.Format.class);
88+
if (mcp != Boolean.TRUE) {
89+
formats.remove(OpenAPIGenerator.Format.MCP);
90+
}
8391
for (var format : OpenAPIGenerator.Format.values()) {
8492
tool.export(result, format, Map.of("adoc", adocPath))
8593
.forEach(output -> getLog().info(" writing: " + output));
@@ -144,4 +152,12 @@ public List<File> getAdoc() {
144152
public void setAdoc(List<File> adoc) {
145153
this.adoc = adoc;
146154
}
155+
156+
public Boolean getMcp() {
157+
return mcp;
158+
}
159+
160+
public void setMcp(Boolean mcp) {
161+
this.mcp = mcp;
162+
}
147163
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.internal.openapi;
7+
8+
import static io.swagger.v3.oas.models.Components.COMPONENTS_SCHEMAS_REF;
9+
10+
import java.util.NoSuchElementException;
11+
import java.util.Optional;
12+
13+
import io.swagger.v3.oas.models.media.Schema;
14+
15+
public class OpenApiSupport {
16+
17+
protected final OpenAPIExt openapi;
18+
19+
public OpenApiSupport(OpenAPIExt openapi) {
20+
this.openapi = openapi;
21+
}
22+
23+
public Schema<?> resolveSchema(Schema<?> schema) {
24+
if (schema.get$ref() != null) {
25+
return resolveSchemaInternal(schema.get$ref())
26+
.orElseThrow(() -> new NoSuchElementException("Schema not found: " + schema.get$ref()));
27+
}
28+
return schema;
29+
}
30+
31+
protected Optional<Schema<?>> resolveSchemaInternal(String name) {
32+
var components = openapi.getComponents();
33+
if (components == null || components.getSchemas() == null) {
34+
throw new NoSuchElementException("No schema found");
35+
}
36+
if (name.startsWith(COMPONENTS_SCHEMAS_REF)) {
37+
name = name.substring(COMPONENTS_SCHEMAS_REF.length());
38+
}
39+
return Optional.ofNullable((Schema<?>) components.getSchemas().get(name));
40+
}
41+
}

modules/jooby-openapi/src/main/java/io/jooby/internal/openapi/asciidoc/AsciiDocContext.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
package io.jooby.internal.openapi.asciidoc;
77

8-
import static io.swagger.v3.oas.models.Components.COMPONENTS_SCHEMAS_REF;
98
import static java.util.Optional.ofNullable;
109

1110
import java.io.IOException;
@@ -29,6 +28,7 @@
2928
import io.jooby.SneakyThrows;
3029
import io.jooby.StatusCode;
3130
import io.jooby.internal.openapi.OpenAPIExt;
31+
import io.jooby.internal.openapi.OpenApiSupport;
3232
import io.pebbletemplates.pebble.PebbleEngine;
3333
import io.pebbletemplates.pebble.error.PebbleException;
3434
import io.pebbletemplates.pebble.extension.AbstractExtension;
@@ -45,7 +45,7 @@
4545
import io.swagger.v3.oas.models.media.NumberSchema;
4646
import io.swagger.v3.oas.models.media.Schema;
4747

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

5151
private ObjectMapper json;
@@ -70,6 +70,7 @@ public class AsciiDocContext {
7070
}
7171

7272
public AsciiDocContext(Path baseDir, ObjectMapper json, ObjectMapper yaml, OpenAPIExt openapi) {
73+
super(openapi);
7374
this.json = json;
7475
this.yamlOpenApi = yaml;
7576
this.yamlOutput = newYamlOutput();
@@ -304,14 +305,6 @@ public String schemaType(Schema<?> schema) {
304305
return Optional.ofNullable(resolved.getFormat()).orElse(resolved.getType());
305306
}
306307

307-
public Schema<?> resolveSchema(Schema<?> schema) {
308-
if (schema.get$ref() != null) {
309-
return resolveSchemaInternal(schema.get$ref())
310-
.orElseThrow(() -> new NoSuchElementException("Schema not found: " + schema.get$ref()));
311-
}
312-
return schema;
313-
}
314-
315308
public Object schemaProperties(Schema<?> schema) {
316309
var resolved = resolveSchema(schema);
317310
if ("array".equals(resolved.getType())) {
@@ -464,17 +457,6 @@ public Schema<?> resolveSchema(String path) {
464457
return schema;
465458
}
466459

467-
private Optional<Schema<?>> resolveSchemaInternal(String name) {
468-
var components = openapi.getComponents();
469-
if (components == null || components.getSchemas() == null) {
470-
throw new NoSuchElementException("No schema found");
471-
}
472-
if (name.startsWith(COMPONENTS_SCHEMAS_REF)) {
473-
name = name.substring(COMPONENTS_SCHEMAS_REF.length());
474-
}
475-
return Optional.ofNullable((Schema<?>) components.getSchemas().get(name));
476-
}
477-
478460
public PebbleEngine getEngine() {
479461
return engine;
480462
}

0 commit comments

Comments
 (0)