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

open-api plugin does not obey "effective documentation" precedence for member structures #2400

Closed
nated0g opened this issue Sep 19, 2024 · 3 comments

Comments

@nated0g
Copy link

nated0g commented Sep 19, 2024

There appears to be a bug in the open-api plugin that leads to incorrectly applied documentation. This is causing some friction for us, requiring refactoring of our model to display documentation correctly. If the plugin behaved according to the documentation here: Effective documentation, we would not have any issue.

To repro:

model.smithy

$version: "2"

namespace example

use aws.api#service
use aws.protocols#restJson1

/// Cutting edge foo barring service, now with AI
@service(sdkId: "Foo")
@restJson1
service FooService {
    version: "2006-03-01"
    operations: [
        BarOperation
    ]
}

/// Performs a bar operation.
@http(uri: "/bar", method: "POST")
operation BarOperation {
    input: BarInput
    output: BarOutput
}

@documentation("It's a Quux")
structure Quux {
    quux: String
}

/// Input for the BarOperation.
structure BarInput {
    @required
    @documentation("It's a Bar")
    bar: Quux
}

/// Output for the BarOperation.
structure BarOutput {
    /// The result of the bar operation.
    result: String
}

smithy-build.json

{
  "version": "1.0",
  "maven": {
    "dependencies": [
      "software.amazon.smithy:smithy-aws-traits:1.51.0",
      "software.amazon.smithy:smithy-openapi:1.51.0"
    ]
  },
  "sources": ["model.smithy"],
  "plugins": {
    "openapi": {
      "service": "example#FooService",
      "protocol": "aws.protocols#restJson1"
    }
  }
}

This produces a model.json that correctly contains "It's a Bar" for the member's documentation:

 "example#BarInput": {
            "type": "structure",
            "members": {
                "bar": {
                    "target": "example#Quux",
                    "traits": {
                        "smithy.api#documentation": "It's a Bar", <-- correct
                        "smithy.api#required": {}
                    }
                }
            },
            "traits": {
                "smithy.api#documentation": "Input for the BarOperation."
            }
        },

however, the generated FooService.openapi.json contains a schema that looks like this:

{
        "schemas": {
            "BarOperationRequestContent": {
                "type": "object",
                "description": "Input for the BarOperation.",
                "properties": {
                    "bar": {
                        "$ref": "#/components/schemas/Quux" <-- missing "description"
                    }
                },
                "required": [
                    "bar"
                ]
            },
            "BarOperationResponseContent": {
                "type": "object",
                "description": "Output for the BarOperation.",
                "properties": {
                    "result": {
                        "type": "string",
                        "description": "The result of the bar operation."
                    }
                }
            },
            "Quux": {
                "type": "object",
                "description": "It's a Quux",
                "properties": {
                    "quux": {
                        "type": "string"
                    }
                }
            }
        }
    }

I expect to see:

                "properties": {
                    "bar": {
                        "$ref": "#/components/schemas/Quux",
                        "description": "It's a Bar" <--
                    }
                }
@nated0g
Copy link
Author

nated0g commented Sep 19, 2024

The example from https://smithy.io/2.0/spec/documentation-traits.html#effective-documentation works correctly. This appears to specifically affect structure members.

milesziemer added a commit to milesziemer/smithy that referenced this issue Sep 23, 2024
Fixes smithy-lang#2400.

When a member targets a structure, it becomes a schema reference when
converted to JSON Schema. Previously, we didn't add member docs to the
converted object, possibly because earlier versions of open api or JSON
Schema did not support it. Reading through [this issue](OAI/OpenAPI-Specification#1514)
the [OAI spec](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#reference-object),
and the [JSON Schema Spec](https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.3),
it seems that OpenAPI 3.1 and JSON Schema 2020-12 support the
`description` property alongside `$ref`. I wasn't able to find anything
about whether it is supported in [JSON Schema 07](https://json-schema.org/draft-07/json-schema-release-notes).
This commit adds a new config option, `addReferenceDescriptions` that
will add the `description` property alongside `$ref` when the member has
documentation. I made it opt-in through the config option so we don't
cause any existing documentation to be changed unexpectedly.
milesziemer added a commit to milesziemer/smithy that referenced this issue Sep 23, 2024
Fixes smithy-lang#2400.

When a member targets a structure, it becomes a schema reference when
converted to JSON Schema. Previously, we didn't add member docs to the
converted object, possibly because earlier versions of open api or JSON
Schema did not support it. Reading through [this issue](OAI/OpenAPI-Specification#1514)
the [OAI spec](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#reference-object),
and the [JSON Schema Spec](https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.3),
it seems that OpenAPI 3.1 and JSON Schema 2020-12 support the
`description` property alongside `$ref`. I wasn't able to find anything
about whether it is supported in [JSON Schema 07](https://json-schema.org/draft-07/json-schema-release-notes).
This commit adds a new config option, `addReferenceDescriptions` that
will add the `description` property alongside `$ref` when the member has
documentation. I made it opt-in through the config option so we don't
cause any existing documentation to be changed unexpectedly.
@milesziemer
Copy link
Contributor

With #2402 you are able to add "addReferenceDescriptions": true to your openapi plugin config so you get member docs on $ref properties

@nated0g
Copy link
Author

nated0g commented Oct 4, 2024

Awesome, thanks!

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

No branches or pull requests

2 participants