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

Don't require AttributeProvider for JsonPropertyInfo-based modifications #56712

Merged
merged 2 commits into from
Jul 10, 2024

Conversation

captainsafia
Copy link
Member

This PR fixes a bug reported by @JamesNK in #56067.

Our OpenAPI implementation currently applies some transformations to the JSON schema produced by System.Text.Json in order to correctly produce $refs for nested types. The application of this fix was guarded behind a check that validated if the JsonPropertyInfo associated with the given schema had an AttributeProvider that we could query, although the AttributeProvider was not strictly needed for this check.

It turns out that for the case of types generated from protobuf-definitions, there's no implementation of IAtributeProvider resolved by System.Text.Json, so the fix was never applied for types used in a gRPC context.

To resolve this issue, we split up the check in TransformSchemaNode to only check for AttributeProvider when we strictly need it.

Below are the before/after for OpenAPI documents generated from the gRPC JsonTranscoding Sandbox app. Note the HelloReply schema.

Before
// 20240709182825
// https://localhost:5001/openapi/v1.json

{
  "openapi": "3.0.1",
  "info": {
    "title": "Sandbox | v1",
    "version": "1.0.0"
  },
  "paths": {
    "/v1/greeter/{name}": {
      "get": {
        "tags": [
          "Greeter"
        ],
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HelloReply"
                }
              }
            }
          },
          "default": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Status"
                }
              }
            }
          }
        }
      }
    },
    "/v1/greeter": {
      "post": {
        "tags": [
          "Greeter"
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/HelloRequestFrom"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HelloReply"
                }
              }
            }
          },
          "default": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Status"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ArrayOfAny": {
        "type": "array",
        "nullable": true
      },
      "HelloReply": {
        "type": "object",
        "properties": {
          "message": {
            "type": "string"
          },
          "nested": {
            "$ref": "#/components/schemas/HelloReply2"
          }
        }
      },
      "HelloReply2": {
        "type": "object",
        "properties": {
          "message": {
            "type": "string"
          },
          "nested": {
            "$ref": "#/components/schemas/#/properties/nested"
          }
        },
        "nullable": true
      },
      "HelloRequestFrom": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "from": {
            "type": "string"
          }
        }
      },
      "Status": {
        "type": "object",
        "properties": {
          "code": {
            "type": "integer",
            "format": "int32",
            "nullable": true
          },
          "message": {
            "type": "string"
          },
          "details": {
            "$ref": "#/components/schemas/ArrayOfAny"
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "Greeter"
    }
  ]
}
After
// 20240709183500
// https://localhost:5001/openapi/v1.json

{
  "openapi": "3.0.1",
  "info": {
    "title": "Sandbox | v1",
    "version": "1.0.0"
  },
  "paths": {
    "/v1/greeter/{name}": {
      "get": {
        "tags": [
          "Greeter"
        ],
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HelloReply"
                }
              }
            }
          },
          "default": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Status"
                }
              }
            }
          }
        }
      }
    },
    "/v1/greeter": {
      "post": {
        "tags": [
          "Greeter"
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/HelloRequestFrom"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HelloReply"
                }
              }
            }
          },
          "default": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Status"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ArrayOfAny": {
        "type": "array",
        "nullable": true
      },
      "HelloReply": {
        "type": "object",
        "properties": {
          "message": {
            "type": "string",
            "nullable": true
          },
          "nested": {
            "$ref": "#/components/schemas/HelloReply"
          }
        }
      },
      "HelloRequestFrom": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "nullable": true
          },
          "from": {
            "type": "string",
            "nullable": true
          }
        }
      },
      "Status": {
        "type": "object",
        "properties": {
          "code": {
            "type": "integer",
            "format": "int32",
            "nullable": true
          },
          "message": {
            "type": "string",
            "nullable": true
          },
          "details": {
            "$ref": "#/components/schemas/ArrayOfAny"
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "Greeter"
    }
  ]
}

@captainsafia captainsafia requested a review from a team as a code owner July 10, 2024 01:45
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Jul 10, 2024
@captainsafia captainsafia added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Jul 10, 2024
Copy link
Member

@JamesNK JamesNK left a comment

Choose a reason for hiding this comment

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

Consider adding a test.

But, assuming gRPC JSON transcoding relies on this, its tests will probably cover it.

@captainsafia
Copy link
Member Author

But, assuming gRPC JSON transcoding relies on this, its tests will probably cover it.

This is the assumption that I was operating on. After a good night's rest though, I was able to figure out a way to simulate this scenario in tests here.

@captainsafia captainsafia enabled auto-merge (squash) July 10, 2024 18:23
@captainsafia captainsafia merged commit dc1a065 into main Jul 10, 2024
26 checks passed
@captainsafia captainsafia deleted the fix-property-attribute-provider branch July 10, 2024 22:18
@dotnet-policy-service dotnet-policy-service bot added this to the 9.0-preview7 milestone Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants