From bdbb2a3d8591c0a566e569a85b904c2889a08c29 Mon Sep 17 00:00:00 2001 From: Conor Gallagher Date: Mon, 7 Mar 2022 17:07:20 +0000 Subject: [PATCH] Enable use of oneOf in additional properties (#117) * Enable use of oneOf in additional properties * comment out test debug line --- .../com/cjbooms/fabrikt/model/OasType.kt | 6 ++--- .../fabrikt/generators/ModelGeneratorTest.kt | 3 +++ .../examples/anyOfOneOfAllOf/api.yaml | 12 +++++++++- .../examples/anyOfOneOfAllOf/models/Models.kt | 22 +++++++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/cjbooms/fabrikt/model/OasType.kt b/src/main/kotlin/com/cjbooms/fabrikt/model/OasType.kt index a93a8bb7..31ee591e 100644 --- a/src/main/kotlin/com/cjbooms/fabrikt/model/OasType.kt +++ b/src/main/kotlin/com/cjbooms/fabrikt/model/OasType.kt @@ -1,10 +1,10 @@ package com.cjbooms.fabrikt.model import com.cjbooms.fabrikt.util.KaizenParserExtensions.isEnumDefinition -import com.cjbooms.fabrikt.util.KaizenParserExtensions.isSimpleMapDefinition -import com.cjbooms.fabrikt.util.KaizenParserExtensions.isSimpleOneOfAnyDefinition import com.cjbooms.fabrikt.util.KaizenParserExtensions.isMapTypeAdditionalProperties import com.cjbooms.fabrikt.util.KaizenParserExtensions.isSchemaLess +import com.cjbooms.fabrikt.util.KaizenParserExtensions.isSimpleMapDefinition +import com.cjbooms.fabrikt.util.KaizenParserExtensions.isSimpleOneOfAnyDefinition import com.cjbooms.fabrikt.util.KaizenParserExtensions.isTypedAdditionalProperties import com.cjbooms.fabrikt.util.KaizenParserExtensions.isUnknownAdditionalProperties import com.cjbooms.fabrikt.util.KaizenParserExtensions.isUntypedAdditionalProperties @@ -72,10 +72,10 @@ sealed class OasType( isEnumDefinition() -> Specialization.ENUM isMapTypeAdditionalProperties(oasKey) -> Specialization.TYPED_MAP_ADDITIONAL_PROPERTIES isSimpleMapDefinition() -> Specialization.MAP - isSimpleOneOfAnyDefinition() -> Specialization.ONE_OF_ANY isTypedAdditionalProperties(oasKey) -> Specialization.TYPED_OBJECT_ADDITIONAL_PROPERTIES isUntypedAdditionalProperties(oasKey) -> Specialization.UNTYPED_OBJECT_ADDITIONAL_PROPERTIES isUnknownAdditionalProperties(oasKey) -> Specialization.UNKNOWN_ADDITIONAL_PROPERTIES + isSimpleOneOfAnyDefinition() -> Specialization.ONE_OF_ANY isSchemaLess() -> Specialization.UNTYPED_OBJECT else -> Specialization.NONE } diff --git a/src/test/kotlin/com/cjbooms/fabrikt/generators/ModelGeneratorTest.kt b/src/test/kotlin/com/cjbooms/fabrikt/generators/ModelGeneratorTest.kt index f242f52b..beaba7d3 100644 --- a/src/test/kotlin/com/cjbooms/fabrikt/generators/ModelGeneratorTest.kt +++ b/src/test/kotlin/com/cjbooms/fabrikt/generators/ModelGeneratorTest.kt @@ -50,6 +50,9 @@ class ModelGeneratorTest { MutableSettings.updateSettings(setOf(CodeGenerationType.HTTP_MODELS), emptySet(), emptySet(), emptySet()) } + // @Test + // fun `debug single test`() = `correct models are generated for different OpenApi Specifications`("insert test case") + @ParameterizedTest @MethodSource("testCases") fun `correct models are generated for different OpenApi Specifications`(testCaseName: String) { diff --git a/src/test/resources/examples/anyOfOneOfAllOf/api.yaml b/src/test/resources/examples/anyOfOneOfAllOf/api.yaml index 047b8206..bb7594ea 100644 --- a/src/test/resources/examples/anyOfOneOfAllOf/api.yaml +++ b/src/test/resources/examples/anyOfOneOfAllOf/api.yaml @@ -71,4 +71,14 @@ components: $ref: '#/components/schemas/AnyValue' AnyValue: - description: Tests support for https://swagger.io/docs/specification/data-models/data-types/#any \ No newline at end of file + description: Tests support for https://swagger.io/docs/specification/data-models/data-types/#any + + OneOfAdditionalProps: + type: object + properties: + second_nested_any_of_prop: + type: string + additionalProperties: + oneOf: + - $ref: '#/components/schemas/FirstOneB' + - $ref: '#/components/schemas/SecondOneB' \ No newline at end of file diff --git a/src/test/resources/examples/anyOfOneOfAllOf/models/Models.kt b/src/test/resources/examples/anyOfOneOfAllOf/models/Models.kt index 6ecb0d9d..248d9703 100644 --- a/src/test/resources/examples/anyOfOneOfAllOf/models/Models.kt +++ b/src/test/resources/examples/anyOfOneOfAllOf/models/Models.kt @@ -1,9 +1,14 @@ package examples.anyOfOneOfAllOf.models +import com.fasterxml.jackson.annotation.JsonAnyGetter +import com.fasterxml.jackson.annotation.JsonAnySetter +import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonProperty import java.math.BigDecimal import kotlin.Any import kotlin.String +import kotlin.collections.Map +import kotlin.collections.MutableMap data class ComplexParent( @param:JsonProperty("oneOf") @@ -59,6 +64,23 @@ data class MoreNesting( val moreNestedPropOne: String? = null ) +data class OneOfAdditionalProps( + @param:JsonProperty("second_nested_any_of_prop") + @get:JsonProperty("second_nested_any_of_prop") + val secondNestedAnyOfProp: String? = null +) { + @get:JsonIgnore + val properties: MutableMap = mutableMapOf() + + @JsonAnyGetter + fun get(): Map = properties + + @JsonAnySetter + fun set(name: String, value: Any) { + properties[name] = value + } +} + data class SecondAnyA( @param:JsonProperty("second_nested_any_of_prop") @get:JsonProperty("second_nested_any_of_prop")