Repository navigation
Expand file tree
/
Copy pathExpOpenAPI.java
More file actions
302 lines (260 loc) · 14.1 KB
/
Copy pathExpOpenAPI.java
File metadata and controls
302 lines (260 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import java.util.*;
// Simula la generación de una spec OpenAPI 3.0 con Java puro.
//
// OpenAPI 2.0 (Swagger) vs 3.0:
// 2.0: "swagger": "2.0" | host + basePath + schemes | body parameter | definitions
// 3.0: "openapi": "3.0.x" | servers[] array | requestBody | components/schemas
//
// springdoc-openapi vs springfox:
// springfox — abandonado en 2021, no soporta Spring Boot 3 (conflictos con Spring MVC 6)
// springdoc — activo, soporta Spring Boot 3, compatible con Jakarta EE
//
// Anotaciones Spring clave (aparecen como comentarios):
// @OpenAPIDefinition — metadatos globales (info, servers, security)
// @Tag — agrupa operaciones en la UI de Swagger
// @Operation — documenta un endpoint (summary, description, operationId)
// @ApiResponse — documenta un código de respuesta con su esquema
// @Parameter — documenta un path/query/header parameter
// @Schema — describe un modelo (tipo, formato, ejemplo, validaciones)
// @SecurityRequirement — indica qué esquema de seguridad requiere la operación
// ── SchemaSpec ────────────────────────────────────────────────────────────────
// @Schema(description = "...", example = "...")
class SchemaSpec {
private final String name;
private final Map<String, String> properties = new LinkedHashMap<>();
private final List<String> required = new ArrayList<>();
SchemaSpec(String name) { this.name = name; }
// Simula @Schema(type = "string", format = "email")
SchemaSpec property(String field, String type, String format, boolean req) {
properties.put(field, format != null ? type + " (format: " + format + ")" : type);
if (req) required.add(field);
return this;
}
String toYaml(int indent) {
String pad = " ".repeat(indent);
StringBuilder sb = new StringBuilder();
sb.append(pad).append(name).append(":\n");
sb.append(pad).append(" type: object\n");
if (!required.isEmpty()) {
sb.append(pad).append(" required: ").append(required).append("\n");
}
sb.append(pad).append(" properties:\n");
properties.forEach((k, v) -> {
sb.append(pad).append(" ").append(k).append(":\n");
String[] parts = v.split(" \\(format: ");
sb.append(pad).append(" type: ").append(parts[0]).append("\n");
if (parts.length > 1) {
sb.append(pad).append(" format: ")
.append(parts[1].replace(")", "")).append("\n");
}
});
return sb.toString();
}
String name() { return name; }
}
// ── EndpointSpec ──────────────────────────────────────────────────────────────
// @Operation(summary = "...", operationId = "...", tags = {...})
// @ApiResponse(responseCode = "200", description = "...", content = @Content(schema = @Schema(ref = "...")))
class EndpointSpec {
private final String method;
private final String path;
private final String summary;
private final String operationId;
private final String tag;
private final List<String[]> parameters = new ArrayList<>(); // [name, in, type, desc]
private final Map<String, String> responses = new LinkedHashMap<>(); // code → description
private String requestBodySchema;
EndpointSpec(String method, String path, String summary, String operationId, String tag) {
this.method = method;
this.path = path;
this.summary = summary;
this.operationId = operationId;
this.tag = tag;
}
// @Parameter(name = "id", in = ParameterIn.PATH, required = true)
EndpointSpec param(String name, String in, String type, String desc) {
parameters.add(new String[]{name, in, type, desc});
return this;
}
// @RequestBody @Schema(ref = "ProductoRequest")
EndpointSpec requestBody(String schemaName) {
this.requestBodySchema = schemaName;
return this;
}
// @ApiResponse(responseCode = "201", description = "Created")
EndpointSpec response(String code, String desc) {
responses.put(code, desc);
return this;
}
String method() { return method; }
String path() { return path; }
String toYaml(int indent) {
String pad = " ".repeat(indent);
StringBuilder sb = new StringBuilder();
sb.append(pad).append(method.toLowerCase()).append(":\n");
sb.append(pad).append(" tags: [").append(tag).append("]\n");
sb.append(pad).append(" summary: ").append(summary).append("\n");
sb.append(pad).append(" operationId: ").append(operationId).append("\n");
if (!parameters.isEmpty()) {
sb.append(pad).append(" parameters:\n");
for (String[] p : parameters) {
sb.append(pad).append(" - name: ").append(p[0]).append("\n");
sb.append(pad).append(" in: ").append(p[1]).append("\n");
sb.append(pad).append(" schema:\n");
sb.append(pad).append(" type: ").append(p[2]).append("\n");
sb.append(pad).append(" description: ").append(p[3]).append("\n");
}
}
if (requestBodySchema != null) {
sb.append(pad).append(" requestBody:\n");
sb.append(pad).append(" required: true\n");
sb.append(pad).append(" content:\n");
sb.append(pad).append(" application/json:\n");
sb.append(pad).append(" schema:\n");
sb.append(pad).append(" $ref: '#/components/schemas/").append(requestBodySchema).append("'\n");
}
sb.append(pad).append(" responses:\n");
responses.forEach((code, desc) -> {
sb.append(pad).append(" '").append(code).append("':\n");
sb.append(pad).append(" description: ").append(desc).append("\n");
});
return sb.toString();
}
}
// ── ControllerSpec ────────────────────────────────────────────────────────────
// Equivale a la configuración de OpenAPI que agrega springdoc al escanear
// un @RestController anotado con @Tag.
//
// GroupedOpenApi — divide la documentación en grupos (v1/v2, por módulo)
// OpenApiCustomizer — personaliza la spec programáticamente (añadir esquemas globales,
// cabeceras de seguridad, etc.)
class ControllerSpec {
private final String title;
private final String version;
private final String serverUrl;
// securityScheme: nombre → tipo (bearerAuth → http/bearer, apiKey → apiKey/header)
private final String securityScheme;
private final Map<String, List<EndpointSpec>> paths = new LinkedHashMap<>();
private final List<SchemaSpec> schemas = new ArrayList<>();
ControllerSpec(String title, String version, String serverUrl, String securityScheme) {
this.title = title;
this.version = version;
this.serverUrl = serverUrl;
this.securityScheme = securityScheme;
}
ControllerSpec addEndpoint(EndpointSpec ep) {
paths.computeIfAbsent(ep.path(), k -> new ArrayList<>()).add(ep);
return this;
}
ControllerSpec addSchema(SchemaSpec schema) {
schemas.add(schema);
return this;
}
// Simula la salida de springdoc en formato pseudo-YAML (no es YAML real,
// sino una aproximación legible que muestra la estructura de la spec).
void printSpec() {
System.out.println("openapi: '3.0.3'");
System.out.println("info:");
System.out.println(" title: " + title);
System.out.println(" version: '" + version + "'");
System.out.println("servers:");
System.out.println(" - url: " + serverUrl);
// @SecurityRequirement — referencia el esquema definido en components/securitySchemes
System.out.println("security:");
System.out.println(" - " + securityScheme + ": []");
System.out.println("paths:");
paths.forEach((path, endpoints) -> {
System.out.println(" " + path + ":");
endpoints.forEach(ep -> System.out.print(ep.toYaml(4)));
});
System.out.println("components:");
System.out.println(" securitySchemes:");
System.out.println(" " + securityScheme + ":");
System.out.println(" type: http");
System.out.println(" scheme: bearer");
System.out.println(" bearerFormat: JWT");
System.out.println(" schemas:");
schemas.forEach(s -> System.out.print(s.toYaml(4)));
}
}
// ── Main ──────────────────────────────────────────────────────────────────────
public class ExpOpenAPI {
public static void main(String[] args) {
System.out.println("═".repeat(60));
System.out.println(" ExpOpenAPI — Swagger/OpenAPI 3.0 con springdoc");
System.out.println("═".repeat(60));
System.out.println();
// ── Diferencias 2.0 vs 3.0 ──────────────────────────────────────────
System.out.println("── OpenAPI 2.0 (Swagger) vs 3.0 ─────────────────────────────");
System.out.println(" 2.0 host + basePath + schemes → 3.0 servers[]");
System.out.println(" 2.0 body parameter → 3.0 requestBody (separado)");
System.out.println(" 2.0 definitions → 3.0 components/schemas");
System.out.println(" 2.0 \"swagger\": \"2.0\" → 3.0 \"openapi\": \"3.0.x\"");
System.out.println(" 2.0 produces/consumes global → 3.0 content type por operación");
System.out.println();
// ── springdoc vs springfox ───────────────────────────────────────────
System.out.println("── springdoc-openapi vs springfox ───────────────────────────");
System.out.println(" springfox: último release en 2020, incompatible con Spring Boot 3");
System.out.println(" springdoc: mantenido activamente, soporta Jakarta EE + Boot 3");
System.out.println(" Migración: reemplazar @ApiOperation/@ApiParam → @Operation/@Parameter");
System.out.println();
// ── Construcción de la spec ──────────────────────────────────────────
SchemaSpec productoRequest = new SchemaSpec("ProductoRequest")
.property("nombre", "string", null, true)
.property("precio", "number", "double", true)
.property("email", "string", "email", false);
SchemaSpec productoResponse = new SchemaSpec("ProductoResponse")
.property("id", "integer", "int64", true)
.property("nombre", "string", null, true)
.property("precio", "number", "double", true);
EndpointSpec listar = new EndpointSpec(
"GET", "/api/productos",
"Listar todos los productos", "listarProductos", "Productos"
).response("200", "Lista de productos").response("401", "No autenticado");
EndpointSpec crear = new EndpointSpec(
"POST", "/api/productos",
"Crear un producto", "crearProducto", "Productos"
).requestBody("ProductoRequest")
.response("201", "Producto creado")
.response("400", "Datos inválidos")
.response("401", "No autenticado");
EndpointSpec obtener = new EndpointSpec(
"GET", "/api/productos/{id}",
"Obtener producto por ID", "obtenerProducto", "Productos"
).param("id", "path", "integer", "ID del producto")
.response("200", "Producto encontrado")
.response("404", "No encontrado");
EndpointSpec eliminar = new EndpointSpec(
"DELETE", "/api/productos/{id}",
"Eliminar producto", "eliminarProducto", "Productos"
).param("id", "path", "integer", "ID del producto")
.response("204", "Eliminado correctamente")
.response("404", "No encontrado");
ControllerSpec spec = new ControllerSpec(
"Java Prep API", "1.0.0",
"http://localhost:8080", "bearerAuth"
);
spec.addEndpoint(listar)
.addEndpoint(crear)
.addEndpoint(obtener)
.addEndpoint(eliminar)
.addSchema(productoRequest)
.addSchema(productoResponse);
System.out.println("── Spec generada (pseudo-YAML) ──────────────────────────────");
System.out.println();
spec.printSpec();
System.out.println();
System.out.println("── Customización con springdoc ──────────────────────────────");
System.out.println(" GroupedOpenApi.builder().group(\"v1\").pathsToMatch(\"/api/v1/**\")");
System.out.println(" → genera /v3/api-docs/v1 y una UI separada por grupo");
System.out.println();
System.out.println(" OpenApiCustomizer: bean que recibe OpenAPI y lo modifica");
System.out.println(" → añadir headers globales, inyectar schemas extra, etc.");
System.out.println();
System.out.println(" operationId convention: por defecto = methodName#N");
System.out.println(" → sobreescribir con @Operation(operationId = \"crearProducto\")");
System.out.println(" para que los clientes generados tengan nombres legibles");
System.out.println();
System.out.println("═".repeat(60));
}
}