From 209e1eb01ed65941b3b75a0065f6a39d63cb62ab Mon Sep 17 00:00:00 2001 From: eanea Date: Tue, 10 Oct 2023 18:42:39 +0300 Subject: [PATCH] 3234: add a test --- .../expected_nullable_option_class_field.yml | 54 +++++++++++++++++++ .../tapir/docs/openapi/VerifyYamlTest.scala | 14 +++++ 2 files changed, 68 insertions(+) create mode 100644 docs/openapi-docs/src/test/resources/expected_nullable_option_class_field.yml diff --git a/docs/openapi-docs/src/test/resources/expected_nullable_option_class_field.yml b/docs/openapi-docs/src/test/resources/expected_nullable_option_class_field.yml new file mode 100644 index 0000000000..0a514d8901 --- /dev/null +++ b/docs/openapi-docs/src/test/resources/expected_nullable_option_class_field.yml @@ -0,0 +1,54 @@ +openapi: 3.1.0 +info: + title: ClassWithOptionClassField + version: '1.0' +paths: + /: + post: + operationId: postRoot + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/ClassWithOptionClassField' + required: true + responses: + '200': + description: '' + content: + text/plain: + schema: + type: string + '400': + description: 'Invalid value for: body' + content: + text/plain: + schema: + type: string +components: + schemas: + Bar: + required: + - bar + type: object + properties: + bar: + type: integer + format: int32 + ClassWithOptionClassField: + required: + - requiredStringField + type: object + properties: + optionalIntField: + required: + - bar + type: + - object + - 'null' + properties: + bar: + type: integer + format: int32 + requiredStringField: + type: string diff --git a/docs/openapi-docs/src/test/scalajvm/sttp/tapir/docs/openapi/VerifyYamlTest.scala b/docs/openapi-docs/src/test/scalajvm/sttp/tapir/docs/openapi/VerifyYamlTest.scala index 52bbdd0391..2ea39b775a 100644 --- a/docs/openapi-docs/src/test/scalajvm/sttp/tapir/docs/openapi/VerifyYamlTest.scala +++ b/docs/openapi-docs/src/test/scalajvm/sttp/tapir/docs/openapi/VerifyYamlTest.scala @@ -666,6 +666,20 @@ class VerifyYamlTest extends AnyFunSuite with Matchers { actualYamlNoIndent shouldBe expectedYaml } + test("should mark optional class fields as nullable by inlining them when configured to do so") { + case class Bar(bar: Int) + case class ClassWithOptionClassField(optionalIntField: Option[Bar], requiredStringField: String) + + val e = endpoint.in(jsonBody[ClassWithOptionClassField]).out(stringBody).post + val expectedYaml = load("expected_nullable_option_class_field.yml") + + val options = OpenAPIDocsOptions.default.copy(markOptionsAsNullable = true) + + val actualYaml = OpenAPIDocsInterpreter(options).toOpenAPI(e, Info("ClassWithOptionClassField", "1.0")).toYaml + val actualYamlNoIndent = noIndentation(actualYaml) + actualYamlNoIndent shouldBe expectedYaml + } + test("should generate default and example values for nested optional fields") { case class Nested(nestedValue: String) case class ClassWithNestedOptionalField(