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

feat: Generate Description from Scaladoc #499

Merged
merged 5 commits into from
Sep 10, 2022

Conversation

Javakky-pxv
Copy link
Collaborator

@Javakky-pxv Javakky-pxv commented Sep 5, 2022

Close #498

As discussed in the issue above, it is a bit risky, but at least in our product, Scaladoc documents and OpenAPI documents have the same scope, so we think it is necessary.

@Javakky-pxv Javakky-pxv force-pushed the javakky/scaladoc-to-description-schama branch from cbd639a to eb8be52 Compare September 6, 2022 02:47
@Javakky-pxv Javakky-pxv force-pushed the javakky/scaladoc-to-description-schama branch 3 times, most recently from 489eecd to b634519 Compare September 6, 2022 07:30
@Javakky-pxv Javakky-pxv force-pushed the javakky/scaladoc-to-description-schama branch from b634519 to 8644ecb Compare September 6, 2022 07:41
@Javakky-pxv
Copy link
Collaborator Author

@kailuowang
Please, review.

@Javakky-pxv
Copy link
Collaborator Author

Need a schema description?

Using runtime-scaladoc-reader, a description can be generated from Scaladoc comments written in the case class.

⚠️ Schema generation from documentation comments is very useful, but should never be used if the scope of scaladoc documentation is different from the scope of OpenAPI documentation.

Add the required dependencies and Compiler Plugin to build.sbt and configure it for use.

embedScaladoc := true
addCompilerPlugin("com.github.takezoe" %% "runtime-scaladoc-reader" % "1.0.3")
libraryDependencies +=  "com.github.takezoe" %% "runtime-scaladoc-reader" % "1.0.3"

For example, a case class might be written as follows.

/**
  * @param name e.g. Sunday, Monday, TuesDay...
  */
case class DayOfWeek(name: String)

The generated JSON will look like this.

{
  "DayOfWeek": {
    "properties": {
      "name": {
        "type": "string",
        "description": "e.g. Sunday, Monday, TuesDay..."
      }
    },
    "required": [
      "name"
    ]
  }
}

@Javakky-pxv Javakky-pxv marked this pull request as ready for review September 6, 2022 08:34
@kailuowang
Copy link
Member

Just want to understand more about the 2 things

  1. Is the runtime-scaladoc-reader library dependency necessary for users?
  2. Is the markdown going to be rendered correctly in HTML? (does swagger UI support rendering markdown?

@Javakky-pxv
Copy link
Collaborator Author

@kailuowang
Thanks for your comment!

  1. Is the runtime-scaladoc-reader library dependency necessary for users?

Yes, if the user wants to use the Scaladoc embed feature, it is a must. However, if this option is not used, it is not necessary to include it in the dependency.

  1. Is the markdown going to be rendered correctly in HTML? (does swagger UI support rendering markdown?

Yes, I have. The Swagger spec says the following about description.
In case you are wondering, I have not actually checked all the specifications, so we have not ruled out the possibility that some MarkDowns may be broken.

Rich Text Formatting

Throughout the specification description fields are noted as supporting CommonMark markdown formatting. Where OpenAPI tooling renders rich text it MUST support, at a minimum, markdown syntax as described by CommonMark 0.27. Tooling MAY choose to ignore some CommonMark features to address security concerns.

https://swagger.io/specification/

@kailuowang
Copy link
Member

Yes, if the user wants to use the Scaladoc embed feature, it is a must.

What puzzles me is that sbt-play-swagger works during compilation time only, Itself is not in the classpath during the play app's runtime. Shouldn't the runtime-scaladoc-reader be needed only for sbt-play-swagger's own runtime (which's in the play app's compilation)?

@Javakky-pxv
Copy link
Collaborator Author

@kailuowang
I am not familiar with the issue, but from the behavior it appears that SwaggerSpecRunner#getClass#getClassLoader is referencing a loaded dependency of the target product.
If so, wouldn't the target project need a runtime-scaladoc-reader?
(Even if it is in the play-swagger dependency, it seems natural that it would not be on ClassLoader or Compiler Plugin unless it is included in the target product's dependencies.)

@Javakky-pxv
Copy link
Collaborator Author

Do you need a deeper investigation into this issue?

@kailuowang
Copy link
Member

kailuowang commented Sep 7, 2022 via email

@Javakky-pxv
Copy link
Collaborator Author

@kailuowang
This test is very interesting and I will try it tomorrow.
Be that as it may, as far as I have tried with my product using publishLocal.

  • No compiler plugin → @scaladoc is not embedded
  • No dependencies only → Error on sbt run.
  • Put both → generated correctly.
    I have tried both with sbt run and it works fine.

@Javakky-pxv
Copy link
Collaborator Author

Or is generate-docs-from-scala-doc only needed to verify that it works, and leaving it as a test case is excessive?

@kailuowang
Copy link
Member

I see. Sounds like that the lib dependency is needed unless we can tweak the class loader (which might be beneficial anyway see #497). I suggest leaving generate-docs-from-scala-doc as a test case. It can also serve as an example for this feature.

@Javakky-pxv
Copy link
Collaborator Author

@kailuowang
Thank you.
I will address the test cases tomorrow.
I am not familiar enough with sbt's ClassLoader, so I would like to study it and work on this issue as well.

I'm going to bed today. Thank you again.

@Javakky-pxv
Copy link
Collaborator Author

@kailuowang
I created a test folder to try it out.
I hope you will take a look at it.

Copy link
Member

@kailuowang kailuowang left a comment

Choose a reason for hiding this comment

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

Thanks so much for this meticulous PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Definitions description based on Scaladoc
2 participants