Skip to content

Commit

Permalink
Issue OpenAPITools#795: Add microprofile OpenAPI annotations for quar…
Browse files Browse the repository at this point in the history
…kus library in JaxRsSpec
  • Loading branch information
Nuno Borges authored and Nuno Borges committed May 3, 2023
1 parent b4f31b9 commit 8778ce0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@

public class CodegenSecurity {
public String name;
public String description;
public String type;
public String scheme;
public Boolean isBasic, isOAuth, isApiKey;
public Boolean isBasic, isOAuth, isApiKey, isOpenId;
// is Basic is true for all http authentication type.
// Those are to differentiate basic and bearer authentication
// isHttpSignature is to support HTTP signature authorization scheme.
Expand All @@ -42,12 +43,15 @@ public class CodegenSecurity {
public String flow, authorizationUrl, tokenUrl, refreshUrl;
public List<Map<String, Object>> scopes;
public Boolean isCode, isPassword, isApplication, isImplicit;
// OpenId specific
public String openIdConnectUrl;

// Return a copy of the security object, filtering out any scopes from the passed-in list.
public CodegenSecurity filterByScopeNames(List<String> filterScopes) {
CodegenSecurity filteredSecurity = new CodegenSecurity();
// Copy all fields except the scopes.
filteredSecurity.name = name;
filteredSecurity.description = description;
filteredSecurity.type = type;
filteredSecurity.isBasic = isBasic;
filteredSecurity.isBasicBasic = isBasicBasic;
Expand All @@ -67,6 +71,7 @@ public CodegenSecurity filterByScopeNames(List<String> filterScopes) {
filteredSecurity.tokenUrl = tokenUrl;
filteredSecurity.authorizationUrl = authorizationUrl;
filteredSecurity.refreshUrl = refreshUrl;
filteredSecurity.openIdConnectUrl = openIdConnectUrl;
// It is not possible to deep copy the extensions, as we have no idea what types they are.
// So the filtered method *will* refer to the original extensions, if any.
filteredSecurity.vendorExtensions = new HashMap<String, Object>(vendorExtensions);
Expand All @@ -93,6 +98,7 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
CodegenSecurity that = (CodegenSecurity) o;
return Objects.equals(name, that.name) &&
Objects.equals(description, that.description) &&
Objects.equals(type, that.type) &&
Objects.equals(scheme, that.scheme) &&
Objects.equals(isBasic, that.isBasic) &&
Expand All @@ -115,22 +121,25 @@ public boolean equals(Object o) {
Objects.equals(isCode, that.isCode) &&
Objects.equals(isPassword, that.isPassword) &&
Objects.equals(isApplication, that.isApplication) &&
Objects.equals(isImplicit, that.isImplicit);
Objects.equals(isImplicit, that.isImplicit) &&
Objects.equals(openIdConnectUrl, that.openIdConnectUrl);
}

@Override
public int hashCode() {

return Objects.hash(name, type, scheme, isBasic, isOAuth, isApiKey,
return Objects.hash(name, description, type, scheme, isBasic, isOAuth, isApiKey,
isBasicBasic, isHttpSignature, isBasicBearer, bearerFormat, vendorExtensions,
keyParamName, isKeyInQuery, isKeyInHeader, isKeyInCookie, flow,
authorizationUrl, tokenUrl, refreshUrl, scopes, isCode, isPassword, isApplication, isImplicit);
authorizationUrl, tokenUrl, refreshUrl, scopes, isCode, isPassword, isApplication, isImplicit,
openIdConnectUrl);
}

@Override
public String toString() {
final StringBuffer sb = new StringBuffer("CodegenSecurity{");
sb.append("name='").append(name).append('\'');
sb.append("description='").append(description).append('\'');
sb.append(", type='").append(type).append('\'');
sb.append(", scheme='").append(scheme).append('\'');
sb.append(", isBasic=").append(isBasic);
Expand All @@ -154,6 +163,7 @@ public String toString() {
sb.append(", isPassword=").append(isPassword);
sb.append(", isApplication=").append(isApplication);
sb.append(", isImplicit=").append(isImplicit);
sb.append(", openIdConnectUrl=").append(openIdConnectUrl);
sb.append('}');
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5321,6 +5321,11 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecurityScheme> securitySc
once(LOGGER).warn("Unknown scheme `{}` found in the HTTP security definition.", securityScheme.getScheme());
}
codegenSecurities.add(cs);
} else if (SecurityScheme.Type.OPENIDCONNECT.equals(securityScheme.getType())) {
final CodegenSecurity cs = defaultCodegenSecurity(key, securityScheme);
cs.isOpenId = true;
cs.openIdConnectUrl = securityScheme.getOpenIdConnectUrl();
codegenSecurities.add(cs);
} else if (SecurityScheme.Type.OAUTH2.equals(securityScheme.getType())) {
final OAuthFlows flows = securityScheme.getFlows();
boolean isFlowEmpty = true;
Expand Down Expand Up @@ -5374,8 +5379,9 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecurityScheme> securitySc
private CodegenSecurity defaultCodegenSecurity(String key, SecurityScheme securityScheme) {
final CodegenSecurity cs = CodegenModelFactory.newInstance(CodegenModelType.SECURITY);
cs.name = key;
cs.description = securityScheme.getDescription();
cs.type = securityScheme.getType().toString();
cs.isCode = cs.isPassword = cs.isApplication = cs.isImplicit = false;
cs.isCode = cs.isPassword = cs.isApplication = cs.isImplicit = cs.isOpenId = false;
cs.isHttpSignature = false;
cs.isBasicBasic = cs.isBasicBearer = false;
cs.scheme = securityScheme.getScheme();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,34 @@ import {{javaxPackage}}.validation.Valid;{{/useBeanValidation}}
license = @License(name = "{{{name}}}", url = "{{{url}}}"){{/license}}{{#contact}},
contact = @Contact(name = "{{{name}}}", email = "{{{email}}}"){{/contact}}
),
tags = {
@Tag(name="{{{tagName}}}", description="{{{tagDescription}}}")
}){{/useMicroProfileOpenAPIAnnotations}}{{#useSwaggerAnnotations}}
tags = @Tag(name="{{{tagName}}}", description="{{{tagDescription}}}")
){{#hasAuthMethods}}
@SecuritySchemes(value = {
{{#authMethods}}{{#isOAuth}}@SecurityScheme(securitySchemeName = "{{name}}", type = SecuritySchemeType.OAUTH2,
description = "{{description}}",{{#isImplicit}}
flows = @OAuthFlows(implicit = @OAuthFlow(authorizationUrl = "{{authorizationUrl}}", tokenUrl = "{{tokenUrl}}", refreshUrl = "{{refreshUrl}}", scopes = { {{#scopes}} @OAuthScope(name = "{{scope}}"){{^-last}},{{/-last}} {{/scopes}} })) {{/isImplicit}}{{#isCode}}
flows = @OAuthFlows(authorizationCode = @OAuthFlow(authorizationUrl = "{{authorizationUrl}}", tokenUrl = "{{tokenUrl}}", refreshUrl = "{{refreshUrl}}", scopes = { {{#scopes}} @OAuthScope(name = "{{scope}}"){{^-last}},{{/-last}} {{/scopes}} })) {{/isCode}}{{#isPassword}}
flows = @OAuthFlows(password = @OAuthFlow(authorizationUrl = "{{authorizationUrl}}", tokenUrl = "{{tokenUrl}}", refreshUrl = "{{refreshUrl}}", scopes = { {{#scopes}} @OAuthScope(name = "{{scope}}"){{^-last}},{{/-last}} {{/scopes}} })) {{/isPassword}}{{#isApplication}}
flows = @OAuthFlows(clientCredentials = @OAuthFlow(authorizationUrl = "{{authorizationUrl}}", tokenUrl = "{{tokenUrl}}", refreshUrl = "{{refreshUrl}}", scopes = { {{#scopes}} @OAuthScope(name = "{{scope}}"){{^-last}},{{/-last}} {{/scopes}} })){{/isApplication}}
){{^-last}},{{/-last}}{{/isOAuth}}{{#isApiKey}}
@SecurityScheme(securitySchemeName = "{{name}}", type = SecuritySchemeType.APIKEY,
description = "{{description}}",
apiKeyName = "{{keyParamName}}",
{{#isKeyInHeader}}in = SecuritySchemeIn.HEADER{{/isKeyInHeader}}{{#isKeyInQuery}}in = SecuritySchemeIn.QUERY{{/isKeyInQuery}}{{#isKeyInCookie}}in = SecuritySchemeIn.COOKIE{{/isKeyInCookie}}
){{^-last}},{{/-last}}{{/isApiKey}}{{#isBasicBasic}}
@SecurityScheme(securitySchemeName = "{{name}}", type = SecuritySchemeType.HTTP,
description = "{{description}}",
scheme = "basic"
){{^-last}},{{/-last}}{{/isBasicBasic}}{{#isBasicBearer}}
@SecurityScheme(securitySchemeName = "{{name}}", type = SecuritySchemeType.HTTP,
description = "{{description}}",
scheme = "bearer", bearerFormat = "{{bearerFormat}}"
){{^-last}},{{/-last}}{{/isBasicBearer}}{{#isOpenId}}
@SecurityScheme(securitySchemeName = "{{name}}", type = SecuritySchemeType.OPENIDCONNECT,
description = "{{description}}",
openIdConnectUrl = "{{openIdConnectUrl}}"
){{^-last}},{{/-last}}{{/isOpenId}}{{/authMethods}}
}){{/hasAuthMethods}}{{/useMicroProfileOpenAPIAnnotations}}{{#useSwaggerAnnotations}}
@Api(description = "the {{{baseName}}} API"){{/useSwaggerAnnotations}}
@Path("{{commonPath}}"){{#hasConsumes}}
@Consumes({ {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}} }){{/hasConsumes}}{{#hasProduces}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
{{/implicitHeadersParams.0}}
@ApiResponses(value = { {{#responses}}
@ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{baseType}}}.class{{#returnContainer}}, responseContainer = "{{{.}}}"{{/returnContainer}}){{^-last}},{{/-last}}{{/responses}} }){{/useSwaggerAnnotations}}{{#useMicroProfileOpenAPIAnnotations}}
{{#hasAuthMethods}}@SecurityRequirements(value={
{{#hasAuthMethods}}@SecurityRequirements(value = {
{{#authMethods}}{{#isOAuth}}@SecurityRequirement(name = "{{name}}", scopes = { {{#scopes}} "{{scope}}"{{^-last}},{{/-last}} {{/scopes}} }){{^-last}},{{/-last}}{{/isOAuth}}{{^isOAuth}} @SecurityRequirement(name = "{{name}}"){{^-last}},{{/-last}}{{/isOAuth}}
{{/authMethods}} }){{/hasAuthMethods}}
@Operation(operationId = "{{{operationId}}}", summary = "{{{summary}}}", description = "{{{notes}}}")
Expand All @@ -29,7 +29,7 @@
@APIResponse(responseCode = "{{{code}}}", description = "{{{message}}}", {{#responseHeaders.0}}headers = { {{#responseHeaders}}
@Header(name = "{{{baseName}}}", schema = @Schema(type = {{{vendorExtensions.x-microprofile-open-api-schema-type}}}), description = "{{{description}}}"){{^-last}},{{/-last}}{{/responseHeaders}}
},{{/responseHeaders.0}} content = { {{#produces}}
@Content(mediaType="{{{mediaType}}}", schema = @Schema(implementation = {{{baseType}}}.class{{#vendorExtensions.x-microprofile-open-api-return-schema-container}}, type = {{{.}}} {{/vendorExtensions.x-microprofile-open-api-return-schema-container}}{{#vendorExtensions.x-microprofile-open-api-return-unique-items}}, uniqueItems = true {{/vendorExtensions.x-microprofile-open-api-return-unique-items}})){{^-last}},{{/-last}}{{/produces}}
@Content(mediaType = "{{{mediaType}}}", schema = @Schema(implementation = {{{baseType}}}.class{{#vendorExtensions.x-microprofile-open-api-return-schema-container}}, type = {{{.}}} {{/vendorExtensions.x-microprofile-open-api-return-schema-container}}{{#vendorExtensions.x-microprofile-open-api-return-unique-items}}, uniqueItems = true {{/vendorExtensions.x-microprofile-open-api-return-unique-items}})){{^-last}},{{/-last}}{{/produces}}
}){{^-last}},{{/-last}}{{/responses}}
}){{/hasProduces}}{{^hasProduces}}
@APIResponses(value = { {{#responses}}
Expand Down

0 comments on commit 8778ce0

Please sign in to comment.