-
Notifications
You must be signed in to change notification settings - Fork 11
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
fix handling of external refs targeting member shapes #189
Merged
+241
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef2da4b
fix handling of external refs targeting member shapes
lewisjkl cab2c05
Add some helper to extract from list
daddykotex 21cc5ad
Properties does not need MatchesOneNames
daddykotex 691b823
Merge pull request #190 from disneystreaming/dfrancoeur/unapply
daddykotex 358169e
update docs
lewisjkl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
modules/openapi/src/internals/postprocess/ExternalMemberRefTransformer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* Copyright 2022 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package smithytranslate.openapi.internals | ||
package postprocess | ||
|
||
import org.typelevel.ci._ | ||
import cats.data.NonEmptyChain | ||
import cats.data.Chain | ||
import scala.annotation.tailrec | ||
|
||
/** This transformer is for the specific case where an external reference | ||
* targets the primitive member of a structure from another file. In this case, | ||
* we need to update the target type to be whatever the target type of the | ||
* referenced member is in the target structure. For example, if there is | ||
* structure A$a that references B$b in another file, we would update A$a to | ||
* instead target the same type that B$b targets, rather than targeting B$b | ||
* directly. Note that this transformation only takes place if B$b targets a | ||
* primitive. If it targets a structure, or another type, then there would be | ||
* no issue. The reason for this is that Structures get rendered as separate | ||
* types already that can be referenced from another file. | ||
*/ | ||
object ExternalMemberRefTransformer extends IModelPostProcessor { | ||
private abstract class MatchesOne(val segment: Segment) { | ||
def unapply(segments: List[Segment]): Option[List[Segment]] = | ||
segments match { | ||
case `segment` :: rest => Some(rest) | ||
case _ => None | ||
} | ||
} | ||
|
||
private trait MatchesOneNamed { self: MatchesOne => | ||
object named { | ||
def unapply(segments: List[Segment]): Option[(Segment, List[Segment])] = | ||
segments match { | ||
case `segment` :: name :: rest => Some((name, rest)) | ||
case _ => None | ||
} | ||
} | ||
} | ||
|
||
private object Components | ||
extends MatchesOne(Segment.Arbitrary(ci"components")) | ||
|
||
private object Schemas | ||
extends MatchesOne(Segment.Arbitrary(ci"schemas")) | ||
with MatchesOneNamed | ||
|
||
private object Properties | ||
extends MatchesOne(Segment.Arbitrary(ci"properties")) | ||
|
||
def apply(model: IModel): IModel = { | ||
val allDefs = model.definitions.map(d => d.id.toString -> d).toMap | ||
|
||
def process(d: Definition): Definition = d match { | ||
case s @ Structure(_, localFields, _, _) => | ||
val newFields = localFields.map { f => | ||
f.tpe.name.segments.toChain.toList match { | ||
case Components(Schemas(_)) => | ||
f.copy(tpe = updatedDefId(f.tpe)) | ||
case _ => f | ||
} | ||
} | ||
s.copy(localFields = newFields) | ||
case other => other | ||
} | ||
|
||
def removeProperties(dId: DefId): Option[DefId] = { | ||
dId.name.segments.toChain.toList match { | ||
case Components(Schemas.named((name, Properties(rest)))) => | ||
Some( | ||
dId.copy(name = | ||
Name( | ||
NonEmptyChain | ||
.of(Components.segment, Schemas.segment, name) | ||
.appendChain(Chain.fromSeq(rest)) | ||
) | ||
) | ||
) | ||
case _ => None | ||
} | ||
} | ||
|
||
@tailrec | ||
def updatedDefId(dId: DefId, isParentProperty: Boolean = false): DefId = | ||
allDefs.get(dId.toString) match { | ||
case Some(Newtype(_, target, _)) => | ||
removeProperties(target) match { | ||
case None => if (isParentProperty) target else dId | ||
case Some(id) => updatedDefId(id, true) | ||
} | ||
case _ => dId | ||
} | ||
IModel(model.definitions.map(process), model.suppressions) | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as I recall from our discussion, this only happens if B$b is a primitive right?
can we state that in the doc or we don't want to because it may happen in other instances and we're not sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I will add it to the doc. It is that way since we match on the NewType on line 95 below and at this point in the postprocessing, all primitives are wrapped in newtypes