Skip to content

Commit

Permalink
fix: Embed top-level $ref before manipulating parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Javakky-pxv committed Sep 6, 2022
1 parent 8ece38a commit 0fc44d3
Showing 1 changed file with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,39 +177,10 @@ final case class SwaggerSpecGenerator(
paths: ListMap[String, JsObject],
baseJson: JsObject = Json.obj()
): JsObject = {
implicit class JsValueUpdate(jsValue: JsValue) {
def update(target: String)(f: JsValue => JsObject): JsValue = jsValue.result match {
case JsDefined(obj: JsObject) =>
JsObject(obj.update(target)(f))

case JsDefined(arr: JsArray) =>
JsArray(arr.value.map(_.update(target)(f)))

case JsDefined(js) => js

case _ => JsNull
}
}

implicit class JsObjectUpdate(jsObject: JsObject) {
def update(target: String)(f: JsValue => JsObject): collection.Seq[(String, JsValue)] = jsObject.fields.flatMap {
case (k, v) if k == target => f(v).fields
case (k, v) => Seq(k -> v.update(target)(f))
}
}

val refKey = "$ref"

val pathsJson = JsObject(paths.values.reduce((acc, p) JsObject(acc.fields ++ p.fields)).update(refKey) {
case JsString(v) =>
val pattern = "^([^#]+)(?:#(?:/[a-zA-Z])+)?$".r
v match {
case pattern(path) if PathValidator.isValid(path) =>
readCfgFile[JsObject](path).getOrElse(JsObject(Seq(refKey -> JsString(v))))
case _ => JsObject(Seq(refKey -> JsString(v)))
}
case v => JsObject(Seq(refKey -> v))
})
val pathsJson = paths.values.reduce((acc, p) JsObject(acc.fields ++ p.fields))

val mainRefs = (pathsJson ++ baseJson) \\ refKey
val customMappingRefs = for {
Expand Down Expand Up @@ -522,11 +493,48 @@ final case class SwaggerSpecGenerator(
case _ Nil
}

for {
val commentsJsonOpt = for {
leadingSpace commentDocLines.headOption.flatMap("""^(\s*)""".r.findFirstIn)
comment = commentDocLines.map(_.drop(leadingSpace.length)).mkString("\n")
result tryParseJson(comment) orElse tryParseYaml(comment)
} yield result

commentsJsonOpt.map { commentsJson =>
implicit class JsValueUpdate(jsValue: JsValue) {
def update(target: String)(f: JsValue => JsObject): JsValue = jsValue.result match {
case JsDefined(obj: JsObject) =>
JsObject(obj.update(target)(f))

case JsDefined(arr: JsArray) =>
JsArray(arr.value.map(_.update(target)(f)))

case JsDefined(js) => js

case _ => JsNull
}
}

implicit class JsObjectUpdate(jsObject: JsObject) {
def update(target: String)(f: JsValue => JsObject): collection.Seq[(String, JsValue)] =
jsObject.fields.flatMap {
case (k, v) if k == target => f(v).fields
case (k, v) => Seq(k -> v.update(target)(f))
}
}

val refKey = "$ref"

JsObject(commentsJson.update(refKey) {
case JsString(v) =>
val pattern = "^([^#]+)(?:#(?:/[a-zA-Z])+)?$".r
v match {
case pattern(path) if PathValidator.isValid(path) =>
readCfgFile[JsObject](path).getOrElse(JsObject(Seq(refKey -> JsString(v))))
case _ => JsObject(Seq(refKey -> JsString(v)))
}
case v => JsObject(Seq(refKey -> v))
})
}
}

val paramsFromComment = jsonFromComment.flatMap(jc (jc \ "parameters").asOpt[JsArray]).map(amendBodyParam)
Expand Down

0 comments on commit 0fc44d3

Please sign in to comment.