Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

#185: Use discriminator fields only in codecs #200

Conversation

jochumb
Copy link
Contributor

@jochumb jochumb commented Aug 26, 2022

  • Moves implicit discriminator mapping to Coproduct creation, instead of Codec creation. To avoid duplication.
  • Removes all discriminator params from model creation.
  • Sets discriminator param and value in codecs.

We have chosen not to make this functionality configurable, to avoid complexity.

This change should be released as a new major version, as it contains breaking changes.

Comment on lines +52 to +60
private def isDiscriminatorProperty(
property: ParameterRef,
coproducts: List[Coproduct]
): Boolean =
coproducts
.flatMap(_.discriminator)
.exists { discriminator =>
property.paramName.term.value == discriminator.fieldName
}
Copy link
Contributor Author

@jochumb jochumb Aug 26, 2022

Choose a reason for hiding this comment

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

Every property that is a discriminator in any of the Product's parents is marked as a discriminator property and thus not created as part of the model.

There could be an edge case in which a Product is a child of multiple Coproducts that define different param names for their discriminators, in which case you might want to have all these params as part of the model.

I see this as an anti-pattern that should be avoided.

Comment on lines 131 to 134
.map {
case EnumValue.StringEv(v) => Lit.String(v)
case EnumValue.IntEv(v) => Lit.Int(v)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might be nicer not to use the Literal here, but the EnumValue itself, however, this adds complexity and we chose not to.

Copy link
Owner

Choose a reason for hiding this comment

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

I think that is a good compromise. We are the ones who generate the enum so we should be fine. Do you mind adding a comment here about that decision?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a comment.

Comment on lines -22 to -23
discriminator:
propertyName: name
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Enum discriminator property without explicit mapping is not supported.

Comment on lines +24 to +26
mapping:
1: "#/components/schemas/Person"
2: "#/components/schemas/Organization"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Int discriminator property without explicit mapping is not supported. So we changed the test to have an explicit mapping.

@jochumb jochumb marked this pull request as ready for review August 26, 2022 11:03
@ghostbuster91
Copy link
Owner

ghostbuster91 commented Sep 3, 2022

Thank you, this is a great work!

We have chosen not to make this functionality configurable, to avoid complexity.

👍

This change should be released as a new major version, as it contains breaking changes.

This project follows semantic versioning. According to the rules versions prior to 1.0.0 do not have to guarantee any compatibility (https://developerexperience.io/practices/semantic-versioning). As this is indeed a breaking change I would like to release it as 0.5.0, if that is ok for you. I would like to not to go with 1.0.0 before making the parser more robust.

@ghostbuster91
Copy link
Owner

I have no idea why the mill integration test is failing but it seems that this is some weird gh quirk. I tested it locally and it passes. It also passes on the CI, just on another branch:https://github.com/ghostbuster91/sttp-openapi-generator/commits/test-ci

…explain decision to use the value instead of the Enum
@jochumb
Copy link
Contributor Author

jochumb commented Sep 5, 2022

I have no idea why the mill integration test is failing but it seems that this is some weird gh quirk. I tested it locally and it passes. It also passes on the CI, just on another branch:https://github.com/ghostbuster91/sttp-openapi-generator/commits/test-ci

It passed locally for me as well, and it is passing now, after my commit.

This change should be released as a new major version, as it contains breaking changes.

This project follows semantic versioning. According to the rules versions prior to 1.0.0 do not have to guarantee any compatibility (https://developerexperience.io/practices/semantic-versioning). As this is indeed a breaking change I would like to release it as 0.5.0, if that is ok for you. I would like to not to go with 1.0.0 before making the parser more robust.

That's great. I didn't know this about Semantic version.

I made a commit to your PR. After some small adjustments it seems that this case is also covered :) Unless, I misunderstood you.

The test for the allOf discriminator is exactly the case I had in mind. It's such a surprise to me that it works without changes, I hadn't even considered to just make the test and see if it runs.

@ghostbuster91 ghostbuster91 merged commit 76b7097 into ghostbuster91:master Sep 7, 2022
@jochumb jochumb deleted the 185-use-discriminator-fields-only-in-codecs branch September 9, 2022 07:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants