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

Maintain route file ordering in SwaggerSpecGenerator #152

Merged
merged 2 commits into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -276,9 +276,15 @@ final case class SwaggerSpecGenerator(

private def paths(routes: Seq[Route], prefix: String, tag: Option[Tag]): JsObject = {
JsObject {
routes.flatMap(endPointEntry(_, prefix, tag))
.groupBy(_._1) // Routes grouped by path
.mapValues(_.map(_._2).reduce(_ deepMerge _))
val endPointEntries = routes.flatMap(route ⇒ endPointEntry(route, prefix, tag))

// maintain the routes order as per the original routing file
val zgbp = endPointEntries.zipWithIndex.groupBy(_._1._1)
import collection.mutable.LinkedHashMap
val lhm = LinkedHashMap(zgbp.toSeq sortBy (_._2.head._2): _*)
val gbp2 = lhm mapValues (_ map (_._1)) toSeq

gbp2.toSeq.map(x ⇒ (x._1, x._2.map(_._2).reduce(_ deepMerge _)))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,29 @@ class SwaggerSpecGeneratorIntegrationSpec extends Specification {
(addTrackJson \ "operationId").as[String] ==== "addPlayedTracks"
}

// TODO: routes order
"should maintain route file order" >> {
// use students routes

val pathJsonObj = pathJson.as[JsObject]
val fieldKeys: Seq[String] = pathJsonObj.fields.map(_._1)
val fieldsWithIndexMap = fieldKeys.zipWithIndex.toMap

val first = fieldsWithIndexMap.get("/api/students/{name}")
val second = fieldsWithIndexMap.get("/api/students/defaultValueParam")
val third = fieldsWithIndexMap.get("/api/students/defaultValueParamString")
val fourth = fieldsWithIndexMap.get("/api/students/defaultValueParamString3")

// all paths must be retrieved
first must beSome[Int]
second must beSome[Int]
third must beSome[Int]
fourth must beSome[Int]

// must be in exact order with nothing inbetween
second.get - first.get === 1
third.get - second.get === 1
Copy link
Member

Choose a reason for hiding this comment

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

This could be written as

val result = List(first, second, third, fourth)
result.forall(_.isDefined) === true
result.orderBy(_.get) === result

Right?

fourth.get - third.get === 1
}

}

Expand Down