All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ancientlightstudios.quarkus.kotlin.openapi.validation.OneOfDiscriminatorMappingCheck.kt Maven / Gradle / Ivy

There is a newer version: 0.4.14
Show newest version
package com.ancientlightstudios.quarkus.kotlin.openapi.validation

import com.ancientlightstudios.quarkus.kotlin.openapi.inspection.inspect
import com.ancientlightstudios.quarkus.kotlin.openapi.models.hints.OriginPathHint.originPath
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformable.TransformableSpec
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformable.components.BaseSchemaComponent
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformable.components.BaseSchemaComponent.Companion.baseSchemaComponent
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformable.components.OneOfComponent
import com.ancientlightstudios.quarkus.kotlin.openapi.utils.SpecIssue

class OneOfDiscriminatorMappingCheck : Check {

    override fun verify(spec: TransformableSpec) {
        val oneOfsWithDiscriminator = spec.schemas.filter {
            it.components.any {
                it is OneOfComponent && it.discriminator != null && it.discriminator.additionalMappings.isNotEmpty()
            }
        }

        oneOfsWithDiscriminator.forEach {
            it.inspect {
                components {
                    // if the oneOf has a discriminator with additional mappings they must match to a $ref in one of the schemas

                    component.discriminator!!.additionalMappings.values.forEach { mapping ->
                        val found = component.schemas.any {
                            val baseSchema = it.schema.baseSchemaComponent()
                            when(baseSchema) {
                                null -> false
                                else -> baseSchema.schema.originPath == mapping
                            }
                        }

                        if (!found) {
                            SpecIssue("No schema found for discriminator mapping $mapping in oneOf schema ${schema.originPath}")
                        }
                    }
                }
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy