有没有一种方法可以从Scala Case Class层次结构中获取JSON-Schema? [英] Is there a way to get a JSON-Schema from a Scala Case Class hierarchy?

查看:276
本文介绍了有没有一种方法可以从Scala Case Class层次结构中获取JSON-Schema?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在记录一个用Scala编写的内部REST API,不幸的是我们无法集成Swagger,因此目前我们正在为doc生成器提供内部解决方案. 我想生成一个JSON-Schema来显示获取资源时的响应情况.我只是想知道是否有任何捷径可以通过利用已经建模的案例类来做到这一点.

I'm documenting an internal REST API written ini Scala, unfortunately we are not able to integrate Swagger, so for now we are going with an in-house solution for the doc generator. I would like to generate a JSON-Schema to show how the response is when getting our resources. I'm just wondering if there is any shortcut to do this by taking advantage of the case classes already modeled.

推荐答案

autoschema 项目能够从Scala案例类中导出JSON模式.您可以按如下方式使用它:

The autoschema project is able to export JSON schema from Scala case classes. You can use it as follows:

case class MyType(myValue: Int)
AutoSchema.createSchema[MyType]

Maven工件似乎不再可用,但是它是Github上可用的SBT项目,因此您可以通过将build.sbt放入以下内容来复制源代码,构建Jar或将其添加为SBT依赖项:

The Maven artifact seems to be no longer available but it is an SBT project available on Github so you can either copy the sources, build a Jar or add it as a dependency with SBT by putting in your build.sbt the following:

lazy val autoschemaProject =
  ProjectRef(uri("https://github.com/coursera/autoschema.git"), "autoschema")

lazy val root = (project in file(".")).dependsOn(autoschemaProject)

我用SBT 0.13.7对此进行了测试.请注意,自动模式具有其自己的依赖性(主要是play-json 2.3.2),因此您可能需要更改其版本,以避免与您自己的项目依赖性发生版本冲突.

I tested this with SBT 0.13.7. Notice that autoschema has its own dependencies (mainly play-json 2.3.2) so you might need to change their versions to avoid version conflicts with you own project dependencies.

这篇关于有没有一种方法可以从Scala Case Class层次结构中获取JSON-Schema?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆