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

Conversation

eanea
Copy link

@eanea eanea commented Oct 10, 2023

fixes #3234

  • inlines the reference type if markOptionsAsNullable = true

@adamw
Copy link
Member

adamw commented Oct 10, 2023

Could you maybe add a test?

test("should mark optional fields as nullable when configured to do so") {
seems like a similar one

@eanea
Copy link
Author

eanea commented Oct 10, 2023

generated schema

image

@eanea
Copy link
Author

eanea commented Oct 10, 2023

Could you maybe add a test?

test("should mark optional fields as nullable when configured to do so") {

seems like a similar one

done :)

@@ -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)
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: optionalObjField

Copy link
Author

Choose a reason for hiding this comment

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

yeah :) fixed it

@eanea eanea force-pushed the 3234/fix_markOptionsAsNullable_for_reference_types branch from 209e1eb to 2b34395 Compare October 11, 2023 13:11
type: object
properties:
optionalObjField:
required:
Copy link
Member

Choose a reason for hiding this comment

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

by inlining the schema we end up with an unused Bar component. Maybe it would be better to generate a one of, e.g.:

    ClassWithOptionClassField:
      required:
      - requiredStringField
      type: object
      properties:
        optionalObjField:
          oneOf:
          - type: 'null'
          - $ref: '#/components/schemas/Bar'
        requiredStringField:
          type: string

?

Copy link
Author

Choose a reason for hiding this comment

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

it'll require changes in the sttp-apispec for all encoders.
I could do it for the circe integration only. will it be acceptable?

Copy link
Author

Choose a reason for hiding this comment

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

@eanea eanea force-pushed the 3234/fix_markOptionsAsNullable_for_reference_types branch from 5a7a72e to 3a39268 Compare October 11, 2023 19:18
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

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!

@adamw adamw merged commit 29f62fc into softwaremill:master Oct 13, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] markOptionsAsNullable doesn't work for reference types
3 participants