Skip to content

Conversation

@seantleonard
Copy link
Contributor

@seantleonard seantleonard commented Jul 6, 2023

Why make this change?

What is this change?

  • improves the mapping between SqlDbType <=> CLR/.NET Framwork (System) types <=> JsonDataType so that OpenApi document generation resolves the appropriate value types in REST endpoint result field value types and input (request body) field value types.

  • To view OpenApi document output, browse to the endpoint /swagger

Before (Swagger view with default values based on resolved JSON data type)

{
  "value": [
    {
      "typeid": 0,
      "byte_types": "string",
      "short_types": 0,
      "int_types": 0,
      "long_types": 0,
      "string_types": "string",
      "single_types": 0,
      "float_types": 0,
      "decimal_types": 0,
      "boolean_types": true,
      "date_types": "Unknown Type: undefined",
      "datetime_types": "Unknown Type: undefined",
      "datetime2_types": "Unknown Type: undefined",
      "datetimeoffset_types": "Unknown Type: undefined",
      "smalldatetime_types": "Unknown Type: undefined",
      "bytearray_types": "string",
      "guid_types": "string"
    }
  ]
}

After (Swagger view with default values based on resolved JSON data type)

{
  "value": [
    {
      "typeid": 0,
      "byte_types": "string",
      "short_types": 0,
      "int_types": 0,
      "long_types": 0,
      "string_types": "string",
      "single_types": 0,
      "float_types": 0,
      "decimal_types": 0,
      "boolean_types": true,
      "date_types": "string",
      "datetime_types": "string",
      "datetime2_types": "string",
      "datetimeoffset_types": "string",
      "smalldatetime_types": "string",
      "bytearray_types": "string",
      "guid_types": "string"
    }
  ]
}

After: OpenApi document json

"components": {
        "schemas": {
            "SupportedType": {
                "type": "object",
                "properties": {
                    "typeid": {
                        "type": "number",
                        "format": ""
                    },
                    "byte_types": {
                        "type": "string",
                        "format": ""
                    },
                    "short_types": {
                        "type": "number",
                        "format": ""
                    },
                    "int_types": {
                        "type": "number",
                        "format": ""
                    },
                    "long_types": {
                        "type": "number",
                        "format": ""
                    },
                    "string_types": {
                        "type": "string",
                        "format": ""
                    },
                    "single_types": {
                        "type": "number",
                        "format": ""
                    },
                    "float_types": {
                        "type": "number",
                        "format": ""
                    },
                    "decimal_types": {
                        "type": "number",
                        "format": ""
                    },
                    "boolean_types": {
                        "type": "boolean",
                        "format": ""
                    },
                    "date_types": {
                        "type": "string",
                        "format": ""
                    },
                    "datetime_types": {
                        "type": "string",
                        "format": ""
                    },
                    "datetime2_types": {
                        "type": "string",
                        "format": ""
                    },
                    "datetimeoffset_types": {
                        "type": "string",
                        "format": ""
                    },
                    "smalldatetime_types": {
                        "type": "string",
                        "format": ""
                    },
                    "bytearray_types": {
                        "type": "string",
                        "format": ""
                    },
                    "guid_types": {
                        "type": "string",
                        "format": ""
                    }
                }
            }
        }
    }

How was this tested?

  • Unit Tests

@ayush3797
Copy link
Contributor

Could you attach a screenshot of how the entity (which is attached in the issue) looks like now in swagger UI?

….tryparse override used since latest syntax eliminates requirement to do null check and casting. Removed "DateTime" and "Time" from TypeHelper._systemTypeToDbTypeMap because they caused GraphQL type tests to fail because of exceptions converting from DateTimeOffset to DateTime. That issue may be fixed by #1473
@seantleonard
Copy link
Contributor Author

Could you attach a screenshot of how the entity (which is attached in the issue) looks like now in swagger UI?

Added before and after to the PR description

…value types to their underlying types, also adds comments to explain in tests and in engine code (TypeHelper)
…ght diffs that this PR actually contributes, as it doesn't change the whole file as github currently suggests. Updates TypeHelper dictionary to be defined by providing an enumerable of key/value pairs instead of indexer assignment.
Copy link
Collaborator

@Aniruddh25 Aniruddh25 left a comment

Choose a reason for hiding this comment

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

LGTM, Thanks for refactoring our type system!

@Aniruddh25
Copy link
Collaborator

Does 0 as the JsonType value in the After correspond to Number?

Copy link
Collaborator

@Aniruddh25 Aniruddh25 left a comment

Choose a reason for hiding this comment

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

Left a question for DateTimeOffset to DbType mapping.

@seantleonard
Copy link
Contributor Author

Does 0 as the JsonType value in the After correspond to Number?

My bad, that was actually a default value (thanks for catching). A default value resolved by swagger UI based on the json data type. I added extra example of the raw openapi schema doc which shows the actual json data type resolved as a result of this change. (this requires extra click throughs in swagger to 'view schema').

@seantleonard seantleonard requested a review from Aniruddh25 July 8, 2023 00:12
Copy link
Collaborator

@Aniruddh25 Aniruddh25 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks again for refactoring our Type system!

…er unexpected sqldbtyp. Also updated configurationtests to not have single line namespace to accurately show changes.
@seantleonard seantleonard enabled auto-merge (squash) July 10, 2023 19:13
@seantleonard seantleonard merged commit daffcd6 into main Jul 10, 2023
@seantleonard seantleonard deleted the dev/seantleonard/openapi_systemtojsondatatypes branch July 10, 2023 19:34
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.

[Bug]: Undefined Types shown in Example Schema for request body in Swagger

6 participants