Browse Source

updated SwaggerConfiguration to simplify request/response bodies annotations

pull/14929/head
dashevchenko 4 months ago
parent
commit
a080bf946b
  1. 31
      application/src/main/java/org/thingsboard/server/config/SwaggerConfiguration.java
  2. 5
      application/src/main/java/org/thingsboard/server/controller/AiModelController.java
  3. 20
      application/src/main/java/org/thingsboard/server/controller/TwoFactorAuthConfigController.java

31
application/src/main/java/org/thingsboard/server/config/SwaggerConfiguration.java

@ -371,6 +371,22 @@ public class SwaggerConfiguration {
}
}
});
// Fix polymorphic request/response bodies: replace inline oneOf with base type $ref
paths.values().stream()
.flatMap(pathItem -> pathItem.readOperationsMap().values().stream())
.forEach(operation -> {
// Request bodies
if (operation.getRequestBody() != null && operation.getRequestBody().getContent() != null) {
replaceInlineOneOfInContent(operation.getRequestBody().getContent(), schemas);
}
// Response bodies
if (operation.getResponses() != null) {
operation.getResponses().values().stream()
.filter(response -> response.getContent() != null)
.forEach(response -> replaceInlineOneOfInContent(response.getContent(), schemas));
}
});
}
var sortedSchemas = new TreeMap<>(openAPI.getComponents().getSchemas());
@ -419,6 +435,21 @@ public class SwaggerConfiguration {
}
private void replaceInlineOneOfInContent(Content content, Map<String, Schema> schemas) {
content.values().forEach(mediaType -> {
Schema<?> schema = mediaType.getSchema();
if (schema != null && schema.getOneOf() != null && !schema.getOneOf().isEmpty()) {
String baseType = findBaseTypeForOneOf(schemas, schema.getOneOf());
if (baseType != null) {
Schema<?> refSchema = new Schema<>();
refSchema.set$ref("#/components/schemas/" + baseType);
mediaType.setSchema(refSchema);
log.debug("Replaced oneOf in content with $ref to {}", baseType);
}
}
});
}
@SuppressWarnings("unchecked")
private void replaceInlineOneOfProperties(Schema<?> schema, Map<String, Schema> allSchemas) {
if (schema == null || schema.getProperties() == null) {

5
application/src/main/java/org/thingsboard/server/controller/AiModelController.java

@ -166,11 +166,6 @@ class AiModelController extends BaseController {
TENANT_AUTHORITY_PARAGRAPH
)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = TbChatResponse.class)))
})
@PostMapping("/chat")
public DeferredResult<TbChatResponse> sendChatRequest(@Valid @RequestBody TbChatRequest tbChatRequest) {
ChatRequest langChainChatRequest = tbChatRequest.toLangChainChatRequest();

20
application/src/main/java/org/thingsboard/server/controller/TwoFactorAuthConfigController.java

@ -102,9 +102,6 @@ public class TwoFactorAuthConfigController extends BaseController {
"}\n```" + NEW_LINE +
"Will throw an error (Bad Request) if the provider is not configured for usage. " +
ControllerConstants.AVAILABLE_FOR_ANY_AUTHORIZED_USER)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = TwoFaAccountConfig.class)))
})
@PostMapping("/account/config/generate")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
public TwoFaAccountConfig generateTwoFaAccountConfig(@Parameter(description = "2FA provider type to generate new account config for", schema = @Schema(defaultValue = "TOTP", requiredMode = Schema.RequiredMode.REQUIRED))
@ -135,8 +132,7 @@ public class TwoFactorAuthConfigController extends BaseController {
ControllerConstants.AVAILABLE_FOR_ANY_AUTHORIZED_USER)
@PostMapping("/account/config/submit")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
public void submitTwoFaAccountConfig(@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(schema = @Schema(implementation = TwoFaAccountConfig.class)))
@Valid @RequestBody TwoFaAccountConfig accountConfig) throws Exception {
public void submitTwoFaAccountConfig(@Valid @RequestBody TwoFaAccountConfig accountConfig) throws Exception {
SecurityUser user = getCurrentUser();
twoFactorAuthService.prepareVerificationCode(user, accountConfig, false);
}
@ -148,10 +144,7 @@ public class TwoFactorAuthConfigController extends BaseController {
ControllerConstants.AVAILABLE_FOR_ANY_AUTHORIZED_USER)
@PostMapping("/account/config")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
public AccountTwoFaSettings verifyAndSaveTwoFaAccountConfig(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "2FA account config to submit for verification",
required = true, content = @Content(mediaType = "application/json",
schema = @Schema(implementation = TwoFaAccountConfig.class)))
@Valid @RequestBody TwoFaAccountConfig accountConfig,
public AccountTwoFaSettings verifyAndSaveTwoFaAccountConfig(@Valid @RequestBody TwoFaAccountConfig accountConfig,
@RequestParam(required = false) String verificationCode) throws Exception {
SecurityUser user = getCurrentUser();
if (twoFaConfigManager.getTwoFaAccountConfig(user.getTenantId(), user.getId(), accountConfig.getProviderType()).isPresent()) {
@ -180,9 +173,6 @@ public class TwoFactorAuthConfigController extends BaseController {
@PutMapping("/account/config")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
public AccountTwoFaSettings updateTwoFaAccountConfig(@RequestParam TwoFaProviderType providerType,
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "2FA account config update request",
required = true, content = @Content(mediaType = "application/json",
schema = @Schema(implementation = TwoFaAccountConfigUpdateRequest.class)))
@RequestBody TwoFaAccountConfigUpdateRequest updateRequest) throws ThingsboardException {
SecurityUser user = getCurrentUser();
@ -210,9 +200,6 @@ public class TwoFactorAuthConfigController extends BaseController {
"```\n[\n \"TOTP\",\n \"EMAIL\",\n \"SMS\"\n]\n```" +
ControllerConstants.AVAILABLE_FOR_ANY_AUTHORIZED_USER
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = TwoFaProviderType.class))))
})
@GetMapping("/providers")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
public List<TwoFaProviderType> getAvailableTwoFaProviders() throws ThingsboardException {
@ -276,7 +263,8 @@ public class TwoFactorAuthConfigController extends BaseController {
ControllerConstants.SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH)
@PostMapping("/settings")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN')")
public PlatformTwoFaSettings savePlatformTwoFaSettings(@RequestBody PlatformTwoFaSettings twoFaSettings) throws ThingsboardException {
public PlatformTwoFaSettings savePlatformTwoFaSettings(@Parameter(description = "Settings value", required = true)
@RequestBody PlatformTwoFaSettings twoFaSettings) throws ThingsboardException {
return twoFaConfigManager.savePlatformTwoFaSettings(getTenantId(), twoFaSettings);
}

Loading…
Cancel
Save