Skip to content

Commit

Permalink
feat(openapi): add converters for various types to handle additional …
Browse files Browse the repository at this point in the history
…properties

- Add BoundedContextConverter to allow additional properties for aggregates
- Update CommandResultConverter to use CommandResultCapable and allow additional properties
- Modify ConditionConverter to set additional properties for options
- Add new converters for WaitSignal, WowMetadata, and MessageHeader
- Update BoundedContextSchemaNameConverter to resolve schema names for non-standard library classes
  • Loading branch information
Ahoo-Wang committed Jan 8, 2025
1 parent 66c7d1d commit 9b93c40
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.wow.openapi.converter

import io.swagger.v3.oas.models.media.Schema
import me.ahoo.wow.configuration.BoundedContext

/**
* BoundedContext Converter
*/
class BoundedContextConverter : TargetTypeModifyConverter() {

override val targetType: Class<*> = BoundedContext::class.java

override fun modify(resolvedSchema: Schema<*>): Schema<*> {
val result = checkNotNull(resolvedSchema.properties[BoundedContext::aggregates.name])
result.additionalProperties = true
result.default = emptyMap<String, Any>()
return resolvedSchema
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import me.ahoo.wow.serialization.JsonSerializer

class BoundedContextSchemaNameConverter : ModelConverter {
companion object {
const val STD_LIB_PREFIX = "java."
fun AnnotatedType.getRawClass(): Class<*>? {
val schemaType = this.type
if (schemaType is Class<*>) {
Expand Down Expand Up @@ -52,14 +53,21 @@ class BoundedContextSchemaNameConverter : ModelConverter {
context: ModelConverterContext,
chain: Iterator<ModelConverter>
): Schema<*>? {
if (type.name.isNullOrBlank()) {
type.getRawClass()?.let {
type.name = it.toSchemaName()
}
}
resolveName(type)
if (chain.hasNext()) {
return chain.next().resolve(type, context, chain)
}
return null
}

private fun resolveName(type: AnnotatedType) {
if (type.name.isNullOrBlank().not()) {
return
}
val rawClass = type.getRawClass() ?: return
if (rawClass.name.startsWith(STD_LIB_PREFIX)) {
return
}
type.name = rawClass.toSchemaName()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package me.ahoo.wow.openapi.converter

import io.swagger.v3.oas.models.media.Schema
import me.ahoo.wow.command.CommandResult
import me.ahoo.wow.command.CommandResultCapable

/**
* CommandResult Converter
Expand All @@ -24,8 +25,8 @@ class CommandResultConverter : TargetTypeModifyConverter() {
override val targetType: Class<*> = CommandResult::class.java

override fun modify(resolvedSchema: Schema<*>): Schema<*> {
val result = checkNotNull(resolvedSchema.properties[CommandResult::result.name])
result.additionalProperties = null
val result = checkNotNull(resolvedSchema.properties[CommandResultCapable::result.name])
result.additionalProperties = true
result.default = emptyMap<String, Any>()
return resolvedSchema
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ class ConditionConverter : TargetTypeModifyConverter() {
resolvedSchema.properties[Condition::operator.name]?.default = Operator.ALL.name
resolvedSchema.properties[Condition::value.name]?.default = EMPTY_VALUE
resolvedSchema.properties[Condition::children.name]?.default = emptyList<Condition>()
resolvedSchema.properties[Condition::options.name]?.default = emptyMap<String, Any>()
resolvedSchema.properties[Condition::options.name]?.let {
it.default = emptyMap<String, Any>()
it.additionalProperties(true)
}
return resolvedSchema
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.wow.openapi.converter

import io.swagger.v3.oas.models.media.Schema
import me.ahoo.wow.api.messaging.Header

/**
* Header Converter
*/
class MessageHeaderConverter : TargetTypeModifyConverter() {

override val targetType: Class<*> = Header::class.java

override fun modify(resolvedSchema: Schema<*>): Schema<*> {
resolvedSchema.additionalProperties = true
return resolvedSchema
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.wow.openapi.converter

import io.swagger.v3.oas.models.media.Schema
import me.ahoo.wow.command.CommandResultCapable
import me.ahoo.wow.command.wait.WaitSignal

/**
* WaitSignal Converter
*/
class WaitSignalConverter : TargetTypeModifyConverter() {

override val targetType: Class<*> = WaitSignal::class.java

override fun modify(resolvedSchema: Schema<*>): Schema<*> {
val result = checkNotNull(resolvedSchema.properties[CommandResultCapable::result.name])
result.additionalProperties = true
result.default = emptyMap<String, Any>()
return resolvedSchema
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.wow.openapi.converter

import io.swagger.v3.oas.models.media.Schema
import me.ahoo.wow.configuration.WowMetadata

/**
* WowMetadata Converter
*/
class WowMetadataConverter : TargetTypeModifyConverter() {

override val targetType: Class<*> = WowMetadata::class.java

override fun modify(resolvedSchema: Schema<*>): Schema<*> {
val result = checkNotNull(resolvedSchema.properties[WowMetadata::contexts.name])
result.additionalProperties = true
result.default = emptyMap<String, Any>()
return resolvedSchema
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ me.ahoo.wow.openapi.converter.EventStreamConverter
me.ahoo.wow.openapi.converter.DomainEventConverter
me.ahoo.wow.openapi.converter.StateEventConverter
me.ahoo.wow.openapi.converter.CommandResultConverter
me.ahoo.wow.openapi.converter.WaitSignalConverter
me.ahoo.wow.openapi.converter.BoundedContextConverter
me.ahoo.wow.openapi.converter.WowMetadataConverter
me.ahoo.wow.openapi.converter.MessageHeaderConverter
me.ahoo.wow.openapi.converter.ConditionConverter
me.ahoo.wow.openapi.converter.ProjectionConverter
me.ahoo.wow.openapi.converter.ListQueryConverter
Expand Down

0 comments on commit 9b93c40

Please sign in to comment.