Skip to content

Commit

Permalink
Adding support for multiple auth clients in OpenAPI Spec
Browse files Browse the repository at this point in the history
  • Loading branch information
computate committed Nov 30, 2023
1 parent b3ac224 commit 92d57e0
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,7 @@ public static String staticSearchFqBaseGenerator(String entityVar, ComputateSite
public static final String[] BaseGeneratorVals = new String[] { configureConfigComplete1, configureConfigFail1 };

public static final String CLASS_SIMPLE_NAME = "BaseGenerator";
public static final String CLASS_API_ADDRESS = "computate-vertx-enUS-BaseGenerator";
public static final String VAR_vertx_ = "vertx_";
public static final String VAR_webClient = "webClient";
public static final String VAR_siteRequest_ = "siteRequest_";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ public static String staticSearchFqSwagger2Generator(String entityVar, Computate
public static final String[] Swagger2GeneratorVals = new String[] { writeApiError1, writeApiError21 };

public static final String CLASS_SIMPLE_NAME = "Swagger2Generator";
public static final String CLASS_API_ADDRESS = "computate-vertx-enUS-Swagger2Generator";


public static String displayNameForClass(String var) {
Expand Down
111 changes: 94 additions & 17 deletions src/gen/java/org/computate/vertx/writer/ApiWriterGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
import io.vertx.core.Promise;
import io.vertx.core.Future;

/**
<ol>
0<h3>Suggestions that can generate more code for you: </h3></ol>
/**
* <ol>
<h3>Suggestions that can generate more code for you: </h3> * </ol>
* <li>You can add a class comment <b>"Api: true"</b> if you wish to GET, POST, PATCH or PUT these ApiWriter objects in a RESTful API.
* </li>
* <h3>About the ApiWriter class and it's generated class ApiWriterGen&lt;Object&gt;: </h3>extends ApiWriterGen
Expand All @@ -79,22 +79,22 @@
* The generated <code>class ApiWriterGen extends Object</code> which means that ApiWriter extends ApiWriterGen which extends Object.
* This generated inheritance is a powerful feature that allows a lot of boiler plate code to be created for you automatically while still preserving inheritance through the power of Java Generic classes.
* </p>
* Api: true
* ApiTag.enUS: true
* ApiUri.enUS: null
* Color: null
* IconGroup: null
* IconName: null
* Indexed: true
* {@inheritDoc}
* <h2>Api: true</h2>
* <h2>ApiTag.enUS: true</h2>
* <h2>ApiUri.enUS: null</h2>
* <h2>Color: null</h2>
* <h2>IconGroup: null</h2>
* <h2>IconName: null</h2>
* <h2>Indexed: true</h2>
* <h2>{@inheritDoc}</h2>
* <p>By adding a class comment "{@inheritDoc}", the ApiWriter class will inherit the helpful inherited class comments from the super class ApiWriterGen.
* </p>
* Rows: null
* Model: true
* Page: true
* SuperPage.enUS: null
* Promise: true
* AName.enUS: null
* <h2>Rows: null</h2>
* <h2>Model: true</h2>
* <h2>Page: true</h2>
* <h2>SuperPage.enUS: null</h2>
* <h2>Promise: true</h2>
* <h2>AName.enUS: null</h2>
* <p>
* Delete the class ApiWriter in Solr:
* curl -k 'https://solr-solr.apps-crc.testing/solr/computate/update?commitWithin=1000&overwrite=true&wt=json' -X POST -H 'Content-type: text/xml' --data-raw '&lt;add&gt;&lt;delete&gt;&lt;query&gt;classeNomCanonique_enUS_indexed_string:org.computate.vertx.writer.ApiWriter&lt;/query&gt;&lt;/delete&gt;&lt;/add&gt;'
Expand All @@ -107,6 +107,7 @@
* Delete the project computate-vertx in Solr:
* curl -k 'https://solr-solr.apps-crc.testing/solr/computate/update?commitWithin=1000&overwrite=true&wt=json' -X POST -H 'Content-type: text/xml' --data-raw '&lt;add&gt;&lt;delete&gt;&lt;query&gt;siteNom_indexed_string:computate\-vertx&lt;/query&gt;&lt;/delete&gt;&lt;/add&gt;'
* </p>
* Generated: true
**/
public abstract class ApiWriterGen<DEV> extends Object {
protected static final Logger LOG = LoggerFactory.getLogger(ApiWriter.class);
Expand Down Expand Up @@ -847,6 +848,66 @@ public static String staticSearchFqConfig(ComputateSiteRequest siteRequest_, Str
return ApiWriter.staticSearchStrConfig(siteRequest_, ApiWriter.staticSearchConfig(siteRequest_, ApiWriter.staticSetConfig(siteRequest_, o)));
}

/////////////////
// authClients //
/////////////////


/** The entity authClients
* is defined as null before being initialized.
*/
@JsonProperty
@JsonInclude(Include.NON_NULL)
protected JsonObject authClients;

/** <br> The entity authClients
* is defined as null before being initialized.
* <br><a href="https://solr-solr.apps-crc.testing/solr/computate/select?q=*:*&fq=partEstEntite_indexed_boolean:true&fq=classeNomCanonique_enUS_indexed_string:org.computate.vertx.writer.ApiWriter&fq=entiteVar_enUS_indexed_string:authClients">Find the entity authClients in Solr</a>
* <br>
* @param c is for wrapping a value to assign to this entity during initialization.
**/
protected abstract void _authClients(Wrap<JsonObject> c);

public JsonObject getAuthClients() {
return authClients;
}

public void setAuthClients(JsonObject authClients) {
this.authClients = authClients;
}
@JsonIgnore
public void setAuthClients(String o) {
this.authClients = ApiWriter.staticSetAuthClients(siteRequest_, o);
}
public static JsonObject staticSetAuthClients(ComputateSiteRequest siteRequest_, String o) {
if(o != null) {
return new JsonObject(o);
}
return null;
}
protected ApiWriter authClientsInit() {
Wrap<JsonObject> authClientsWrap = new Wrap<JsonObject>().var("authClients");
if(authClients == null) {
_authClients(authClientsWrap);
Optional.ofNullable(authClientsWrap.getO()).ifPresent(o -> {
setAuthClients(o);
});
}
return (ApiWriter)this;
}

public static String staticSearchAuthClients(ComputateSiteRequest siteRequest_, JsonObject o) {
return o.toString();
}

public static String staticSearchStrAuthClients(ComputateSiteRequest siteRequest_, String o) {
return o == null ? null : o.toString();
}

public static String staticSearchFqAuthClients(ComputateSiteRequest siteRequest_, String o) {
return ApiWriter.staticSearchStrAuthClients(siteRequest_, ApiWriter.staticSearchAuthClients(siteRequest_, ApiWriter.staticSetAuthClients(siteRequest_, o)));
}

/////////////////////
// wRequestHeaders //
/////////////////////
Expand Down Expand Up @@ -2794,6 +2855,7 @@ public void initApiWriter() {
wRequestBodiesInit();
wSchemasInit();
configInit();
authClientsInit();
wRequestHeadersInit();
wRequestDescriptionInit();
wResponseDescriptionInit();
Expand Down Expand Up @@ -2917,6 +2979,8 @@ public Object obtainApiWriter(String var) {
return oApiWriter.wSchemas;
case "config":
return oApiWriter.config;
case "authClients":
return oApiWriter.authClients;
case "wRequestHeaders":
return oApiWriter.wRequestHeaders;
case "wRequestDescription":
Expand Down Expand Up @@ -3040,6 +3104,8 @@ public static Object staticSetApiWriter(String entityVar, ComputateSiteRequest s
return ApiWriter.staticSetTabsResponses(siteRequest_, o);
case "config":
return ApiWriter.staticSetConfig(siteRequest_, o);
case "authClients":
return ApiWriter.staticSetAuthClients(siteRequest_, o);
case "classApiTag":
return ApiWriter.staticSetClassApiTag(siteRequest_, o);
case "classExtendsBase":
Expand Down Expand Up @@ -3124,6 +3190,8 @@ public static Object staticSearchApiWriter(String entityVar, ComputateSiteReques
return ApiWriter.staticSearchTabsResponses(siteRequest_, (Integer)o);
case "config":
return ApiWriter.staticSearchConfig(siteRequest_, (JsonObject)o);
case "authClients":
return ApiWriter.staticSearchAuthClients(siteRequest_, (JsonObject)o);
case "classApiTag":
return ApiWriter.staticSearchClassApiTag(siteRequest_, (String)o);
case "classExtendsBase":
Expand Down Expand Up @@ -3208,6 +3276,8 @@ public static String staticSearchStrApiWriter(String entityVar, ComputateSiteReq
return ApiWriter.staticSearchStrTabsResponses(siteRequest_, (Integer)o);
case "config":
return ApiWriter.staticSearchStrConfig(siteRequest_, (String)o);
case "authClients":
return ApiWriter.staticSearchStrAuthClients(siteRequest_, (String)o);
case "classApiTag":
return ApiWriter.staticSearchStrClassApiTag(siteRequest_, (String)o);
case "classExtendsBase":
Expand Down Expand Up @@ -3292,6 +3362,8 @@ public static String staticSearchFqApiWriter(String entityVar, ComputateSiteRequ
return ApiWriter.staticSearchFqTabsResponses(siteRequest_, o);
case "config":
return ApiWriter.staticSearchFqConfig(siteRequest_, o);
case "authClients":
return ApiWriter.staticSearchFqAuthClients(siteRequest_, o);
case "classApiTag":
return ApiWriter.staticSearchFqClassApiTag(siteRequest_, o);
case "classExtendsBase":
Expand Down Expand Up @@ -3361,6 +3433,7 @@ public static String staticSearchFqApiWriter(String entityVar, ComputateSiteRequ
}

public static final String CLASS_SIMPLE_NAME = "ApiWriter";
public static final String CLASS_API_ADDRESS = "computate-vertx-enUS-ApiWriter";
public static final String VAR_siteRequest_ = "siteRequest_";
public static final String VAR_classDoc = "classDoc";
public static final String VAR_classSolrDocument = "classSolrDocument";
Expand All @@ -3375,6 +3448,7 @@ public static String staticSearchFqApiWriter(String entityVar, ComputateSiteRequ
public static final String VAR_wRequestBodies = "wRequestBodies";
public static final String VAR_wSchemas = "wSchemas";
public static final String VAR_config = "config";
public static final String VAR_authClients = "authClients";
public static final String VAR_wRequestHeaders = "wRequestHeaders";
public static final String VAR_wRequestDescription = "wRequestDescription";
public static final String VAR_wResponseDescription = "wResponseDescription";
Expand Down Expand Up @@ -3426,6 +3500,7 @@ public static String staticSearchFqApiWriter(String entityVar, ComputateSiteRequ
public static final String DISPLAY_NAME_wRequestBodies = "";
public static final String DISPLAY_NAME_wSchemas = "";
public static final String DISPLAY_NAME_config = "";
public static final String DISPLAY_NAME_authClients = "";
public static final String DISPLAY_NAME_wRequestHeaders = "";
public static final String DISPLAY_NAME_wRequestDescription = "";
public static final String DISPLAY_NAME_wResponseDescription = "";
Expand Down Expand Up @@ -3496,6 +3571,8 @@ public static String displayNameApiWriter(String var) {
return DISPLAY_NAME_wSchemas;
case VAR_config:
return DISPLAY_NAME_config;
case VAR_authClients:
return DISPLAY_NAME_authClients;
case VAR_wRequestHeaders:
return DISPLAY_NAME_wRequestHeaders;
case VAR_wRequestDescription:
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/computate/vertx/config/ComputateConfigKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public class ComputateConfigKeys {
**/
public static final String AUTH_REALM = "AUTH_REALM";

/**
* The Auth clients of the site.
**/
public static final String AUTH_CLIENTS = "AUTH_CLIENTS";

/**
* The Auth client ID of the site.
**/
Expand All @@ -169,6 +174,21 @@ public class ComputateConfigKeys {
**/
public static final String AUTH_SECRET = "AUTH_SECRET";

/**
* The Auth Client callback URI
**/
public static final String AUTH_CALLBACK_URI = "AUTH_CALLBACK_URI";

/**
* The Auth Client logout URI
**/
public static final String AUTH_LOGOUT_URI = "AUTH_LOGOUT_URI";

/**
* The Auth Client OpenAPI ID
**/
public static final String AUTH_OPEN_API_ID = "AUTH_OPEN_API_ID";

/**
* The Auth client secret of the site.
**/
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/computate/vertx/openapi/BaseGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -269,6 +270,7 @@ public Future<Void> loadClass(List<SolrResponse.Doc> docs, Integer i) {
apiWriter.setWSchemas(wSchemas);
apiWriter.setOpenApiVersion(openApiVersion);
apiWriter.setClassUris(classUris);
apiWriter.setAuthClients(Optional.ofNullable(config.getValue(ComputateConfigKeys.AUTH_CLIENTS)).map(v -> v instanceof JsonObject ? (JsonObject)v : new JsonObject(v.toString())).orElse(new JsonObject()));
apiWriter.initDeepApiWriter(siteRequest_);
apiWriters.add(apiWriter);
}
Expand Down
78 changes: 46 additions & 32 deletions src/main/java/org/computate/vertx/openapi/Swagger2Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
*/
package org.computate.vertx.openapi;

import java.util.Optional;

import org.computate.vertx.config.ComputateConfigKeys;

import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;

/**
* InitDeepBefore: true
Expand Down Expand Up @@ -69,45 +72,56 @@ public Future<Void> writeApi() {
Promise<Void> promise = Promise.promise();

try {
JsonObject authClients = Optional.ofNullable(config.getValue(ComputateConfigKeys.AUTH_CLIENTS)).map(v -> v instanceof JsonObject ? (JsonObject)v : new JsonObject(v.toString())).orElse(new JsonObject());

wPaths.tl(0, "paths:");
wPaths.l();
wPaths.tl(1, "/callback:");
wPaths.tl(2, "get:");
wPaths.tl(3, "operationId: callback");
wPaths.tl(3, "x-vertx-event-bus: ", appName, "-", languageName, "-callback");
wPaths.tl(3, "description: >+");
wPaths.tl(3, "responses:");
wPaths.tl(4, "'200':");
wPaths.tl(5, "description: >+");
wPaths.tl(5, "content:");
wPaths.tl(6, "application/json; charset=utf-8:");
wPaths.tl(7, "schema:");
wPaths.tl(8, "type: string");
wPaths.l();
wPaths.tl(1, "/logout:");
wPaths.tl(2, "get:");
wPaths.tl(3, "operationId: logout");
wPaths.tl(3, "x-vertx-event-bus: ", appName, "-", languageName, "-logout");
wPaths.tl(3, "description: >+");
wPaths.tl(3, "responses:");
wPaths.tl(4, "'200':");
wPaths.tl(5, "description: >+");
wPaths.tl(5, "content:");
wPaths.tl(6, "application/json; charset=utf-8:");
wPaths.tl(7, "schema:");
wPaths.tl(8, "type: string");
wPaths.l();
authClients.fieldNames().forEach(authClientOpenApiId -> {
JsonObject authClient = authClients.getJsonObject(authClientOpenApiId);
String authCallbackUri = authClient.getString(ComputateConfigKeys.AUTH_CALLBACK_URI);
String authLogoutUri = authClient.getString(ComputateConfigKeys.AUTH_LOGOUT_URI);

wPaths.l();
wPaths.tl(1, authCallbackUri, ":");
wPaths.tl(2, "get:");
wPaths.tl(3, "operationId: callback");
wPaths.tl(3, "x-vertx-event-bus: ", appName, "-", languageName, "-", authClientOpenApiId, "-callback");
wPaths.tl(3, "description: >+");
wPaths.tl(3, "responses:");
wPaths.tl(4, "'200':");
wPaths.tl(5, "description: >+");
wPaths.tl(5, "content:");
wPaths.tl(6, "application/json; charset=utf-8:");
wPaths.tl(7, "schema:");
wPaths.tl(8, "type: string");
wPaths.l();
wPaths.tl(1, authLogoutUri, ":");
wPaths.tl(2, "get:");
wPaths.tl(3, "operationId: logout");
wPaths.tl(3, "x-vertx-event-bus: ", appName, "-", languageName, "-", authClientOpenApiId, "-logout");
wPaths.tl(3, "description: >+");
wPaths.tl(3, "responses:");
wPaths.tl(4, "'200':");
wPaths.tl(5, "description: >+");
wPaths.tl(5, "content:");
wPaths.tl(6, "application/json; charset=utf-8:");
wPaths.tl(7, "schema:");
wPaths.tl(8, "type: string");
wPaths.l();
});

if(openApiVersionNumber == 2) {
wSchemas.tl(0, "definitions:");
}
else {
wRequestBodies.tl(0, "components:");
if(config.getString(ComputateConfigKeys.AUTH_URL) != null) {
wRequestBodies.tl(1, "securitySchemes:");
wRequestBodies.tl(2, "openIdConnect:");
wRequestBodies.tl(3, "type: openIdConnect");
wRequestBodies.tl(3, "openIdConnectUrl: ", config.getString(ComputateConfigKeys.AUTH_URL), "/realms/", config.getString(ComputateConfigKeys.AUTH_REALM), "/.well-known/openid-configuration");
if(authClients.size() > 0) {
authClients.fieldNames().forEach(authClientOpenApiId -> {
JsonObject authClient = authClients.getJsonObject(authClientOpenApiId);
wRequestBodies.tl(1, "securitySchemes:");
wRequestBodies.tl(2, authClientOpenApiId, ":");
wRequestBodies.tl(3, "type: openIdConnect");
wRequestBodies.tl(3, "openIdConnectUrl: ", authClient.getString(ComputateConfigKeys.AUTH_URL), "/realms/", authClient.getString(ComputateConfigKeys.AUTH_REALM), "/.well-known/openid-configuration");
});
}
wRequestBodies.tl(1, "requestBodies:");

Expand Down
Loading

0 comments on commit 92d57e0

Please sign in to comment.