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

#1808: add external Refs #1831

Merged
merged 3 commits into from
Feb 9, 2022
Merged

#1808: add external Refs #1831

merged 3 commits into from
Feb 9, 2022

Conversation

martinpallmann
Copy link
Contributor

No description provided.

@kubinio123 kubinio123 linked an issue Feb 4, 2022 that may be closed by this pull request
content:
application/json:
schema:
$ref: https://opensource.zalando.com/restful-api-guidelines/models/problem-1.0.1.yaml#/Problem
Copy link
Contributor

Choose a reason for hiding this comment

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

value of $ref should be quoted like 'xxx'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. But this seems to be done somewhere in the YAML serializer. I have no idea how this works. Some help here would be appreciated. I'll have a look but I'm not confident that I can fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This depends on the String Style of the TapirOpenAPICirceYaml.toYaml method. In the Printer class in line 69 is this check:

  private def stringScalarStyle(value: String): DumperOptions.ScalarStyle =
    if (isBad(value)) DumperOptions.ScalarStyle.DOUBLE_QUOTED
    else if (stringStyle == StringStyle.Plain && hasNewline(value)) DumperOptions.ScalarStyle.LITERAL
    else StringStyle.toScalarStyle(stringStyle)

So I'm not sure how to fix this or if it's working as intended.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe the Printer.isBad method needs to be adjusted? I'm not sure...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems the Printer needs to be modified somewhat: https://yaml.org/spec/1.2.2/#733-plain-style

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's handled in such way in the underlying snakeyaml lib since it's used by circe Printer. I see that Printer is not changing the string style for url string. If you are convinced that this is incorrect handling as for yaml definition I think you can try to report issue in snakeyaml, I am not a yaml expert :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm also not an expert there. Do you think this PR can be merged then?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, thanks!

val actualYaml =
OpenAPIDocsInterpreter().toOpenAPI(List(external_reference), Info("Fruits", "1.0")).toYaml

println(actualYaml)
Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove that ;)


case class Problem()

object Problem {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that this class won't be reused in other tests maybe we can put it into companion object in VerifyYamlTest

Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved the class

@@ -5,6 +5,6 @@ import sttp.tapir.{Schema => TSchema}

private[schema] class NameToSchemaReference(nameToKey: Map[TSchema.SName, ObjectKey]) {
def map(name: TSchema.SName): Reference = {
Reference.to("#/components/schemas/", nameToKey(name))
nameToKey.get(name).fold(Reference.to(name.fullName, ""))(key => Reference.to("#/components/schemas/", key))
Copy link
Contributor

Choose a reason for hiding this comment

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

It will be nice to have a comment here describing what's the case when we don't have a entry in the map for the given name, something like:

  def map(name: TSchema.SName): Reference = 
    nameToKey.get(name) match {
      // we have a reference to internal model  
      case Some(key) => Reference.to("#/components/schemas/", key)
      // we have a reference to external model  
      case None => Reference.to(name.fullName, "")
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a comment.

@kubinio123 kubinio123 merged commit 5c91288 into softwaremill:master Feb 9, 2022
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.

[Feature] Add external Reference to Schema
2 participants