Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3234: use markOptionsAsNullable for Option reference types #3235

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ private[schema] class TSchemaToASchema(toSchemaReference: ToSchemaReference, mar
case TSchemaType.SArray(nested @ TSchema(_, Some(name), _, _, _, _, _, _, _, _, _)) =>
ASchema(SchemaType.Array).copy(items = Some(toSchemaReference.map(nested, name)))
case TSchemaType.SArray(el) => ASchema(SchemaType.Array).copy(items = Some(apply(el)))
case TSchemaType.SOption(nested @ TSchema(_, Some(name), _, _, _, _, _, _, _, _, _)) => toSchemaReference.map(nested, name)
case TSchemaType.SOption(nested @ TSchema(_, Some(name), _, _, _, _, _, _, _, _, _)) => {
val ref = toSchemaReference.map(nested, name)
if (!markOptionsAsNullable) ref
else
ASchema.oneOf(List(ref), ref.discriminator).copy(nullable = Some(true))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe doing here simply ASchema.oneOf(List(ref, ASchema(SchemaType.Null), None) would work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this change, let's see if things work :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great! thank you :)
what else is needed to merge the PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing, tests pass :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

}
case TSchemaType.SOption(el) => apply(el, isOptionElement = true)
case TSchemaType.SBinary() => ASchema(SchemaType.String).copy(format = SchemaFormat.Binary)
case TSchemaType.SDate() => ASchema(SchemaType.String).copy(format = SchemaFormat.Date)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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:
optionalClassField:
oneOf:
- $ref: '#/components/schemas/Bar'
- type: 'null'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- type: 'null' requires to merge & use softwaremill/sttp-apispec#118

requiredStringField:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,20 @@ class VerifyYamlTest extends AnyFunSuite with Matchers {
actualYamlNoIndent shouldBe expectedYaml
}

test("should mark optional class fields as nullable when configured to do so") {
case class Bar(bar: Int)
case class ClassWithOptionClassField(optionalObjField: 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(
Expand Down