Skip to content

Commit 614a6f2

Browse files
committed
[PLAT-16521]Create swagger_all.json that includes INTERNAL APIs to be consumed in CLI
Summary: Creating swagger-all.json that contains all APIs (public, preview, deprecated, internal) that can be consumed by CLI and Terraform. Test Plan: Check swagger-all.json file. Check the `client/go/` directory for all the APIs missing due to the YbaApi.Internal tag Reviewers: #yba-api-review, sneelakantan, skurapati Reviewed By: #yba-api-review, sneelakantan Subscribers: yugaware Differential Revision: https://phorge.dev.yugabyte.com/D41370
1 parent a0007bf commit 614a6f2

File tree

6 files changed

+140
-29
lines changed

6 files changed

+140
-29
lines changed

managed/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ devops/pex/pexEnv
7171
# openapi generated server stubs during build
7272
src/main/resources/openapi.yaml
7373
src/main/resources/openapi_public.yaml
74+
src/main/resources/swagger-all.json
7475
tmp
7576

7677
# openapi generated JS API stubs

managed/build.sbt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ lazy val pythonGenV2Client = project.in(file("client/python"))
686686
// Generate a Go API client.
687687
lazy val gogen = project.in(file("client/go"))
688688
.settings(
689-
openApiInputSpec := "src/main/resources/swagger.json",
689+
openApiInputSpec := "src/main/resources/swagger-all.json",
690690
openApiGeneratorName := "go",
691691
openApiOutputDir := "client/go/v1",
692692
openApiGenerateModelTests := SettingDisabled,
@@ -1146,6 +1146,7 @@ lazy val swagger = project
11461146
// Consider generating this only in managedResources
11471147
val swaggerJson = (root / Compile / resourceDirectory).value / "swagger.json"
11481148
val swaggerStrictJson = (root / Compile / resourceDirectory).value / "swagger-strict.json"
1149+
val swaggerAllJson = (root / Compile / resourceDirectory).value / "swagger-all.json"
11491150
Def.sequential(
11501151
(Test / runMain )
11511152
.toTask(s" com.yugabyte.yw.controllers.SwaggerGenTest $swaggerJson"),
@@ -1156,6 +1157,8 @@ lazy val swagger = project
11561157
// or use '--exclude_deprecated all' to drop all deprecated APIs
11571158
(Test / runMain )
11581159
.toTask(s" com.yugabyte.yw.controllers.SwaggerGenTest $swaggerStrictJson --exclude_deprecated all"),
1160+
(Test / runMain )
1161+
.toTask(s" com.yugabyte.yw.controllers.SwaggerGenTest $swaggerAllJson --exclude_internal none")
11591162
)
11601163
}.value,
11611164

managed/swagger/src/test/java/com/yugabyte/yw/common/swagger/PlatformModelConverter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ public class PlatformModelConverter implements ModelConverter {
3131
public static String excludeYbaDeprecatedOption;
3232
private static YBADeprecationProcessor deprecationProcessor;
3333

34+
public static String excludeYbaInternalOption;
35+
private static YBAInternalProcessor internalProcessor;
36+
3437
static void register() {
3538
// remove if one is already registers to avoid duplicates in tests
3639
ModelConverters.getInstance().removeConverter(SINGLETON);
3740
ModelConverters.getInstance().addConverter(SINGLETON);
3841
deprecationProcessor = new YBADeprecationProcessor(excludeYbaDeprecatedOption);
42+
internalProcessor = new YBAInternalProcessor(excludeYbaInternalOption);
3943
}
4044

4145
private static final ImmutableSet<String> SKIPPED_PACKAGES =

managed/swagger/src/test/java/com/yugabyte/yw/common/swagger/PlatformSwaggerSpecFilter.java

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ public class PlatformSwaggerSpecFilter extends AbstractSpecFilter {
1818
private static final String INTERNAL_MARKER = "YbaApi Internal";
1919
private static final String DEPRECATED_MARKER = "Deprecated since YBA version";
2020
private boolean isStrictMode = false;
21+
private boolean isInternalMode = false;
2122

2223
public PlatformSwaggerSpecFilter() {
2324
PlatformModelConverter.register();
2425
isStrictMode = "all".equalsIgnoreCase(PlatformModelConverter.excludeYbaDeprecatedOption);
26+
isInternalMode = "none".equalsIgnoreCase(PlatformModelConverter.excludeYbaInternalOption);
2527
}
2628

2729
@Override
@@ -31,14 +33,17 @@ public boolean isOperationAllowed(
3133
Map<String, List<String>> params,
3234
Map<String, String> cookies,
3335
Map<String, List<String>> headers) {
34-
if (operation.getDescription() != null
35-
&& operation.getDescription().contains(INTERNAL_MARKER)) {
36-
LOG.info("Skipping swagger generation for internal method '{}'", operation.getOperationId());
37-
return false;
36+
if (!isInternalMode) {
37+
if (operation.getDescription() != null
38+
&& operation.getDescription().contains(INTERNAL_MARKER)) {
39+
LOG.info(
40+
"Skipping swagger generation for internal method '{}'", operation.getOperationId());
41+
return false;
42+
}
3843
}
3944
if (isStrictMode) {
40-
if (operation.getDescription() != null
41-
&& operation.getDescription().contains(DEPRECATED_MARKER)) {
45+
if (operation.getDescription() != null
46+
&& operation.getDescription().contains(DEPRECATED_MARKER)) {
4247
LOG.info("Skipping deprecated method in strict mode '{}'", operation.getOperationId());
4348
return false;
4449
}
@@ -54,13 +59,15 @@ public boolean isParamAllowed(
5459
Map<String, List<String>> params,
5560
Map<String, String> cookies,
5661
Map<String, List<String>> headers) {
57-
if (parameter.getDescription() != null
58-
&& parameter.getDescription().contains(INTERNAL_MARKER)) {
59-
LOG.info(
60-
"Skipping swagger generation for internal param '{}' of operation '{}'",
61-
parameter.getName(),
62-
operation.getOperationId());
63-
return false;
62+
if (!isInternalMode) {
63+
if (parameter.getDescription() != null
64+
&& parameter.getDescription().contains(INTERNAL_MARKER)) {
65+
LOG.info(
66+
"Skipping swagger generation for internal param '{}' of operation '{}'",
67+
parameter.getName(),
68+
operation.getOperationId());
69+
return false;
70+
}
6471
}
6572
if (isStrictMode) {
6673
if (parameter.getDescription() != null
@@ -81,9 +88,11 @@ public boolean isDefinitionAllowed(
8188
Map<String, List<String>> params,
8289
Map<String, String> cookies,
8390
Map<String, List<String>> headers) {
84-
if (model.getDescription() != null && model.getDescription().contains(INTERNAL_MARKER)) {
85-
LOG.info("Skipping swagger generation for model '{}'", ((ModelImpl) model).getName());
86-
return false;
91+
if (!isInternalMode) {
92+
if (model.getDescription() != null && model.getDescription().contains(INTERNAL_MARKER)) {
93+
LOG.info("Skipping swagger generation for model '{}'", ((ModelImpl) model).getName());
94+
return false;
95+
}
8796
}
8897
if (isStrictMode) {
8998
if (model.getDescription() != null && model.getDescription().contains(DEPRECATED_MARKER)) {
@@ -102,14 +111,18 @@ public boolean isPropertyAllowed(
102111
Map<String, List<String>> params,
103112
Map<String, String> cookies,
104113
Map<String, List<String>> headers) {
105-
if (property.getDescription() != null && property.getDescription().contains(INTERNAL_MARKER)) {
106-
LOG.info(
107-
"Skipping swagger generation for property '{}' of model '{}'",
108-
property.getName(),
109-
((ModelImpl) model).getName());
110-
return false;
114+
if (!isInternalMode) {
115+
if (property.getDescription() != null
116+
&& property.getDescription().contains(INTERNAL_MARKER)) {
117+
LOG.info(
118+
"Skipping swagger generation for property '{}' of model '{}'",
119+
property.getName(),
120+
((ModelImpl) model).getName());
121+
return false;
122+
}
111123
}
112-
// TODO: move deprecation skipping of model property from PlatformModelConverter to here
124+
// TODO: move deprecation skipping of model property from PlatformModelConverter
125+
// to here
113126
return true;
114127
}
115128
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2023 YugaByte, Inc. and Contributors
3+
*
4+
* Licensed under the Polyform Free Trial License 1.0.0 (the "License"); you
5+
* may not use this file except in compliance with the License. You
6+
* may obtain a copy of the License at
7+
*
8+
* http://github.com/YugaByte/yugabyte-db/blob/master/licenses/POLYFORM-FREE-TRIAL-LICENSE-1.0.0.txt
9+
*/
10+
package com.yugabyte.yw.common.swagger;
11+
12+
import com.google.common.base.Strings;
13+
import lombok.extern.slf4j.Slf4j;
14+
15+
@Slf4j
16+
public class YBAInternalProcessor {
17+
private String excludeYbaInternal;
18+
19+
enum ExcludeKind {
20+
NONE,
21+
ALL,
22+
}
23+
24+
private ExcludeKind excludeKind = ExcludeKind.NONE;
25+
26+
/*
27+
* @param excludeExpression can be set to something like - "all", "none"
28+
*/
29+
public YBAInternalProcessor(String excludeExpression) {
30+
setExcludeInternal(excludeExpression);
31+
}
32+
33+
private void setExcludeInternal(String excludeExpression) {
34+
if (Strings.isNullOrEmpty(excludeExpression)
35+
|| excludeExpression.trim().equalsIgnoreCase("all")) {
36+
excludeKind = ExcludeKind.ALL;
37+
log.info("Setting Internal to exclude ALL");
38+
return;
39+
}
40+
if (excludeExpression.trim().equalsIgnoreCase("none")) {
41+
excludeKind = ExcludeKind.NONE;
42+
log.info("Setting Internal to exclude NONE");
43+
return;
44+
}
45+
throwInvalidExcludeExpression(excludeExpression);
46+
}
47+
48+
private void throwInvalidExcludeExpression(String excludeExpression) {
49+
String errMsg = String.format("--exclude-internal=%s is invalid", excludeExpression);
50+
throw new RuntimeException(errMsg);
51+
}
52+
53+
public boolean shouldExcludeInternal() {
54+
switch (excludeKind) {
55+
case ALL:
56+
return true;
57+
case NONE:
58+
return false;
59+
default:
60+
return true; // exclude all internal APIs by default
61+
}
62+
}
63+
64+
// For unit testing
65+
ExcludeKind getExcludeKind() {
66+
return excludeKind;
67+
}
68+
}

managed/swagger/src/test/java/com/yugabyte/yw/controllers/SwaggerGenTest.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,16 @@ private void sortTagsList(ObjectNode specJsonNode) {
346346

347347
public static void main(String[] args)
348348
throws IOException, NoSuchFieldException, IllegalAccessException {
349-
String expectedSwagger = getCurrentSpec(args[0]);
350349
excludeDeprecated(args);
350+
excludeInternal(args);
351+
// Not checking swagger has changed for swagger-all.json
352+
Boolean checkSwaggerHasChanged = true;
353+
if (PlatformModelConverter.excludeYbaInternalOption != null) {
354+
if (PlatformModelConverter.excludeYbaInternalOption.length() != 0
355+
&& PlatformModelConverter.excludeYbaInternalOption.toLowerCase().equals("none")) {
356+
checkSwaggerHasChanged = false;
357+
}
358+
}
351359
SwaggerGenTest swaggerGenTest = new SwaggerGenTest();
352360
try {
353361
swaggerGenTest.startServer();
@@ -364,10 +372,13 @@ public static void main(String[] args)
364372
}
365373
System.out.println("Generating swagger spec:" + Arrays.toString(args));
366374
String swaggerSpec = swaggerGenTest.getSwaggerSpec();
367-
if (expectedSwagger.length() == swaggerSpec.length()) {
368-
// TODO: Fix this: Only length comparison because the json ordering change
369-
System.out.println("Swagger Specs have not changed");
370-
return;
375+
if (checkSwaggerHasChanged) {
376+
String expectedSwagger = getCurrentSpec(args[0]);
377+
if (expectedSwagger.length() == swaggerSpec.length()) {
378+
// TODO: Fix this: Only length comparison because the json ordering change
379+
System.out.println("Swagger Specs have not changed");
380+
return;
381+
}
371382
}
372383
try (FileWriter fileWriter = new FileWriter(new File(args[0]))) {
373384
// todo only generate on change
@@ -393,6 +404,17 @@ private static void excludeDeprecated(String[] args) {
393404
PlatformModelConverter.excludeYbaDeprecatedOption = "";
394405
if (args.length > 2 && args[1].equalsIgnoreCase("--exclude_deprecated")) {
395406
PlatformModelConverter.excludeYbaDeprecatedOption = args[2];
407+
} else if (args.length > 4 && args[3].equalsIgnoreCase("--exclude_deprecated")) {
408+
PlatformModelConverter.excludeYbaDeprecatedOption = args[4];
409+
}
410+
}
411+
412+
private static void excludeInternal(String[] args) {
413+
PlatformModelConverter.excludeYbaInternalOption = "";
414+
if (args.length > 2 && args[1].equalsIgnoreCase("--exclude_internal")) {
415+
PlatformModelConverter.excludeYbaInternalOption = args[2];
416+
} else if (args.length > 4 && args[3].equalsIgnoreCase("--exclude_internal")) {
417+
PlatformModelConverter.excludeYbaInternalOption = args[4];
396418
}
397419
}
398420
}

0 commit comments

Comments
 (0)