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

fix(flatten): allows the use of pointers to schemas in x-... extensions #89

Merged
merged 1 commit into from
Dec 22, 2023

Conversation

fredbi
Copy link
Member

@fredbi fredbi commented Dec 21, 2023

This PR allows for reusing schemas stored in x-... extensions of the spec, via json pointers.

We have seen some specs using such extensions to create "alternate definitions". The spec flattening canonicalizes such schemas by reintroducing them as #/definitions, so they can be consumed by codegen.

@fredbi fredbi force-pushed the fix/1898-extra-props-in-spec branch from f7563d0 to 45eca10 Compare December 21, 2023 13:55
Copy link

codecov bot commented Dec 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b9463f9) 83.37% compared to head (887ddcd) 92.68%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #89      +/-   ##
==========================================
+ Coverage   83.37%   92.68%   +9.30%     
==========================================
  Files          14        7       -7     
  Lines        2490     1899     -591     
==========================================
- Hits         2076     1760     -316     
+ Misses        364      101     -263     
+ Partials       50       38      -12     
Flag Coverage Δ
oldstable 92.68% <100.00%> (+9.30%) ⬆️
stable 92.68% <100.00%> (+9.30%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fredbi
Copy link
Member Author

fredbi commented Dec 21, 2023

Here is the spec from go-swagger/go-swagger#1898 used for testing this feature.
Before this patch, the x-... section unmarshals as an interface{}
and therefore cannot be properly processed with $ref and schema.
With the new flattening, I could test (manually) that go-swagger generates a proper model for this additional schema and generates a proper server and client (more tests on the follow-up PR on go-swagger to include this change).

{
  "swagger": "2.0",
  "info": {
    "title": "example.proto",
    "version": "version not set"
  },
  "schemes": [
    "http",
    "https"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/example/v2/GetEvents": {
      "get": {
        "operationId": "GetEvents",
        "responses": {
          "200": {
            "description": "A successful response.(streaming responses)",
            "schema": {
              "$ref": "#/x-stream-definitions/v2EventMsg"
            }
          }
        },
        "parameters": [
          {
            "name": "afterEventID",
            "description": ".",
            "in": "query",
            "required": false,
            "type": "string"
          }
        ],
        "tags": [
          "Matchmaking"
        ]
      }
    }
  },
  "definitions": {
    "protobufAny": {
      "type": "object",
      "properties": {
        "type_url": {
          "type": "string"
        },
        "value": {
          "type": "string",
          "format": "byte"
        }
      }
    },
    "runtimeStreamError": {
      "type": "object",
      "properties": {
        "grpc_code": {
          "type": "integer",
          "format": "int32"
        },
        "http_code": {
          "type": "integer",
          "format": "int32"
        },
        "message": {
          "type": "string"
        },
        "http_status": {
          "type": "string"
        },
        "details": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/protobufAny"
          }
        }
      }
    },
    "v2EventMsg": {
      "type": "object",
      "properties": {
        "eventID": {
          "type": "string"
        }
      }
    }
  },
  "x-stream-definitions": {
    "v2EventMsg": {
      "type": "object",
      "properties": {
        "result": {
          "$ref": "#/definitions/v2EventMsg"
        },
        "error": {
          "$ref": "#/definitions/runtimeStreamError"
        }
      },
      "title": "Stream result of v2EventMsg"
    }
  }
}

This PR allows for reusing schemas stored in x-... extensions of the
spec, via json pointers.

We have seen some specs using such extensions to create "alternate
definitions". The spec flattening canonicalizes such schemas by
reintroducing them as #/definitions, so they can be consumed by codegen.

* requires: go-openapi/jsonpointer#16
* contributes: go-swagger/go-swagger#1898

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@fredbi fredbi force-pushed the fix/1898-extra-props-in-spec branch from 6b1acfe to 887ddcd Compare December 21, 2023 17:13
@fredbi fredbi marked this pull request as ready for review December 21, 2023 17:14
@fredbi fredbi merged commit 9c9799f into go-openapi:master Dec 22, 2023
10 checks passed
@fredbi fredbi deleted the fix/1898-extra-props-in-spec branch December 22, 2023 10:27
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.

2 participants