yaml-language-server icon indicating copy to clipboard operation
yaml-language-server copied to clipboard

Treat OpenAPI descriptions as markdown

Open remcohaszing opened this issue 4 years ago • 10 comments

What does this PR do?

This treats descriptions in OpenAPI documents as markdown as per https://spec.openapis.org/oas/latest.html#rich-text-formatting

What issues does this PR fix or reference?

It fixes a specific, yet common use case for https://github.com/redhat-developer/vscode-yaml/issues/454

Is it tested? How?

A test was added.

remcohaszing avatar Sep 02 '21 10:09 remcohaszing

Coverage Status

Coverage increased (+0.1%) to 78.529% when pulling 466a7edbfbe83506f704209687205557d60f0b5c on remcohaszing:openapi-markdown-descriptions into 6df37d38dfe7bedce12aee4175fbe7da05b8e333 on redhat-developer:main.

coveralls avatar Sep 02 '21 10:09 coveralls

Would you also accept if I add a property to schema settings to force enable markdown descriptions? I see now that this is very easy.

  export interface SchemasSettings {
    priority?: SchemaPriority;document then highest priority wins
    fileMatch: string[];
+   markdownDescriptions?: boolean
    schema?: unknown;
    uri: string;
  }
        if (schema && node && !schema.errors.length) {
-         const descriptionsAreMarkdown = 'openapi' in schema.schema || 'swagger' in schema.schema;
+         const descriptionsAreMarkdown = schema.markdownDescriptions || 'openapi' in schema.schema || 'swagger' in schema.schema;
          const matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset);

This would make it possible to specify JSON schema descriptions are markdown in monaco-yaml. Also although it wouldn’t resolve the referenced issue yet, but it would be a first step towards a fix.

remcohaszing avatar Sep 03 '21 12:09 remcohaszing

Can we take a different approach and make toMarkdown function more clever so that it does not make the changes that it applies when it is not necessary?

gorkem avatar Sep 04 '21 18:09 gorkem

toMarkdown could simply take descriptionsAreMarkdown as a second argument, and return the raw input if it’s this property is true. Although IMO this doesn’t make the code simpler or more readable.

remcohaszing avatar Sep 04 '21 20:09 remcohaszing

I was actually thinking if we could make toMarkdown to detect if the string is already Markdown and skip modifying it. Unfortunately, I was not successful to spot an easy way to do that with my glorious 3 min spend on Google :)

gorkem avatar Sep 08 '21 21:09 gorkem

I was actually thinking if we could make toMarkdown to detect if the string is already Markdown and skip modifying it. Unfortunately, I was not successful to spot an easy way to do that with my glorious 3 min spend on Google :)

I’m pretty sure it’s not. :disappointed: It would have been awesome though.

I have also been thinking maybe all descriptions could be treated as markdown, but that would mess up code sample for example.

For example, the following probably represents a code sample in plain text:

This is a code sample
function foo() {
  console.log('bar');
}

However, in markdown, this is equivalent to:

This is a code sample function foo() { console.log('bar'); }

remcohaszing avatar Sep 10 '21 18:09 remcohaszing

I am a bit reluctant on this one because we are introducing a behavior based on schema. We did that in the early times with kubernetes and it has been haunting us since. We are actually planning to remove it. Can we think of a way to introduce this without yaml-language-server being aware of the schema?

gorkem avatar Oct 03 '21 15:10 gorkem

The alternative is what I proposed in https://github.com/redhat-developer/yaml-language-server/pull/536#issuecomment-912499323. This makes the hover provider only aware of whether or not it should treat descriptions as markdown, but not why. This moves the responsibility of determining whether or not a schema has markdown descriptions up.

remcohaszing avatar Oct 04 '21 08:10 remcohaszing

I am more comfortable with that one, instead of yaml-language-server being aware of the schemas. I am afraid the list of schemas may grow out of hand quickly.

gorkem avatar Oct 04 '21 14:10 gorkem

I looked into moving this check up, but I’m not sure where exactly to put this then. I prefer keeping this as-is for now, as I know this works.

remcohaszing avatar Oct 12 '21 13:10 remcohaszing