diff --git a/.gitignore b/.gitignore index 6e6d124..069611e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ test-operation.yaml .idea/ +*.iml \ No newline at end of file diff --git a/datamodel/low/v3/create_document_test.go b/datamodel/low/v3/create_document_test.go index be43430..b6439c9 100644 --- a/datamodel/low/v3/create_document_test.go +++ b/datamodel/low/v3/create_document_test.go @@ -669,6 +669,52 @@ externalDocs: assert.Len(t, err, 1) } +func TestCreateDocument_YamlAnchor(t *testing.T) { + // load petstore into bytes + anchorDocument, _ := os.ReadFile("../../../test_specs/yaml-anchor.yaml") + + // read in specification + info, _ := datamodel.ExtractSpecInfo(anchorDocument) + + // build low-level document model + document, errors := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{ + AllowFileReferences: false, + AllowRemoteReferences: false, + }) + + // if something went wrong, a slice of errors is returned + if len(errors) > 0 { + for i := range errors { + fmt.Printf("error: %s\n", errors[i].Error()) + } + panic("cannot build document") + } + + examplePath := document.Paths.Value.FindPath("/system/examples/{id}") + assert.NotNil(t, examplePath) + + // Check tag reference + getOp := examplePath.Value.Get.Value + assert.NotNil(t, getOp) + postOp := examplePath.Value.Get.Value + assert.NotNil(t, postOp) + assert.Equal(t, 1, len(getOp.GetTags().Value)) + assert.Equal(t, 1, len(postOp.GetTags().Value)) + assert.Equal(t, getOp.GetTags().Value, postOp.GetTags().Value) + + // Check paramter reference + + getParams := examplePath.Value.Get.Value.Parameters.Value + assert.NotNil(t, getParams) + postParams := examplePath.Value.Post.Value.Parameters.Value + assert.NotNil(t, postParams) + + assert.Equal(t, 1, len(getParams)) + assert.Equal(t, 1, len(postParams)) + assert.Equal(t, getParams, postParams) + +} + func ExampleCreateDocument() { // How to create a low-level OpenAPI 3 Document diff --git a/test_specs/yaml-anchor.yaml b/test_specs/yaml-anchor.yaml new file mode 100644 index 0000000..88e89ba --- /dev/null +++ b/test_specs/yaml-anchor.yaml @@ -0,0 +1,85 @@ +openapi: 3.0.2 +info: + title: Example + version: 0.0.1 +components: + securitySchemes: + bearerAuth: + type: http + scheme: bearer + bearerFormat: JWT + schemas: + Error: + type: object + properties: + message: + type: string + description: Error message + Example: + type: object + required: + - id + properties: + id: + type: string + title: Name + pattern: ^[a-zA-Z0-9_\-]+$ + description: Name of the Example. + description: + type: string + title: Description + description: Brief description of this Example. Optional. +security: + - bearerAuth: [] +paths: + /system/examples/{id}: + get: + tags: + &a1 + - Examples + summary: Get a list of Example objects + parameters: + &id + - name: id + in: path + required: true + schema: + type: string + description: Unique ID + description: Get a list of Example objects + responses: + &a2 + "200": + description: a list of Example objects + content: + application/json: + schema: + type: object + properties: + count: + type: integer + description: number of items present in the items array + items: + type: array + items: + $ref: "#/components/schemas/Example" + "401": + description: Unauthorized + "500": + description: Unexpected error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + post: + tags: *a1 + parameters: *id + summary: Create Example + description: Create Example + requestBody: + description: New Example object + content: + application/json: + schema: + $ref: "#/components/schemas/Example" + responses: *a2 \ No newline at end of file