|
6 | 6 |
|
7 | 7 | "github.com/speakeasy-api/openapi/jsonschema/oas3" |
8 | 8 | "github.com/speakeasy-api/openapi/marshaller" |
| 9 | + "github.com/stretchr/testify/assert" |
9 | 10 | "github.com/stretchr/testify/require" |
10 | 11 | ) |
11 | 12 |
|
@@ -343,3 +344,234 @@ x-metadata: |
343 | 344 | require.True(t, ok) |
344 | 345 | require.NotNil(t, ext.Value) |
345 | 346 | } |
| 347 | + |
| 348 | +func TestSchema_Unmarshal_MergeKeys_Success(t *testing.T) { |
| 349 | + t.Parallel() |
| 350 | + |
| 351 | + tests := []struct { |
| 352 | + name string |
| 353 | + yml string |
| 354 | + expectedProps []string |
| 355 | + checkOverride bool |
| 356 | + overrideKey string |
| 357 | + overrideDesc string |
| 358 | + }{ |
| 359 | + { |
| 360 | + name: "merge key in properties inherits from anchor", |
| 361 | + yml: ` |
| 362 | +type: object |
| 363 | +properties: |
| 364 | + id: &base_id |
| 365 | + type: integer |
| 366 | + description: Base ID |
| 367 | + name: |
| 368 | + type: string |
| 369 | + description: User name |
| 370 | +`, |
| 371 | + expectedProps: []string{"id", "name"}, |
| 372 | + }, |
| 373 | + { |
| 374 | + name: "merge key expands aliased mapping into properties", |
| 375 | + yml: ` |
| 376 | +type: object |
| 377 | +properties: |
| 378 | + <<: |
| 379 | + id: |
| 380 | + type: integer |
| 381 | + description: Base ID |
| 382 | + name: |
| 383 | + type: string |
| 384 | + description: Base name |
| 385 | + email: |
| 386 | + type: string |
| 387 | + description: Email address |
| 388 | +`, |
| 389 | + expectedProps: []string{"id", "name", "email"}, |
| 390 | + }, |
| 391 | + { |
| 392 | + name: "merge key with alias expands into properties", |
| 393 | + // NOTE: YAML anchors/aliases must be defined in the same document. |
| 394 | + // We define the anchor at a top-level key and reference it in properties. |
| 395 | + yml: ` |
| 396 | +type: object |
| 397 | +x-base-props: &base_props |
| 398 | + id: |
| 399 | + type: integer |
| 400 | + description: Base ID |
| 401 | + name: |
| 402 | + type: string |
| 403 | + description: Base name |
| 404 | +properties: |
| 405 | + <<: *base_props |
| 406 | + email: |
| 407 | + type: string |
| 408 | + description: Email address |
| 409 | +`, |
| 410 | + expectedProps: []string{"id", "name", "email"}, |
| 411 | + }, |
| 412 | + { |
| 413 | + name: "merge key with override in properties", |
| 414 | + yml: ` |
| 415 | +type: object |
| 416 | +x-base-props: &base_props |
| 417 | + id: |
| 418 | + type: integer |
| 419 | + description: Base ID |
| 420 | + name: |
| 421 | + type: string |
| 422 | + description: Base name |
| 423 | +properties: |
| 424 | + <<: *base_props |
| 425 | + name: |
| 426 | + type: string |
| 427 | + description: Overridden name |
| 428 | + email: |
| 429 | + type: string |
| 430 | + description: Email address |
| 431 | +`, |
| 432 | + expectedProps: []string{"id", "name", "email"}, |
| 433 | + checkOverride: true, |
| 434 | + overrideKey: "name", |
| 435 | + overrideDesc: "Overridden name", |
| 436 | + }, |
| 437 | + } |
| 438 | + |
| 439 | + for _, tt := range tests { |
| 440 | + t.Run(tt.name, func(t *testing.T) { |
| 441 | + t.Parallel() |
| 442 | + |
| 443 | + var schema oas3.Schema |
| 444 | + |
| 445 | + validationErrs, err := marshaller.Unmarshal(t.Context(), bytes.NewBufferString(tt.yml), &schema) |
| 446 | + require.NoError(t, err, "unmarshal should succeed") |
| 447 | + require.Empty(t, validationErrs, "should have no validation errors") |
| 448 | + |
| 449 | + require.NotNil(t, schema.Properties, "properties should not be nil") |
| 450 | + |
| 451 | + _, hasMergeKey := schema.Properties.Get("<<") |
| 452 | + assert.False(t, hasMergeKey, "merge key '<<' should not appear as a property name") |
| 453 | + |
| 454 | + for _, prop := range tt.expectedProps { |
| 455 | + propSchema, ok := schema.Properties.Get(prop) |
| 456 | + assert.True(t, ok, "property %q should exist", prop) |
| 457 | + assert.NotNil(t, propSchema, "property %q schema should not be nil", prop) |
| 458 | + } |
| 459 | + |
| 460 | + assert.Equal(t, len(tt.expectedProps), schema.Properties.Len(), "should have expected number of properties") |
| 461 | + |
| 462 | + if tt.checkOverride { |
| 463 | + propSchema, ok := schema.Properties.Get(tt.overrideKey) |
| 464 | + require.True(t, ok, "override property %q should exist", tt.overrideKey) |
| 465 | + require.True(t, propSchema.IsSchema(), "override property %q should be a schema", tt.overrideKey) |
| 466 | + assert.Equal(t, tt.overrideDesc, propSchema.GetSchema().GetDescription(), "overridden property should have the overriding description") |
| 467 | + } |
| 468 | + }) |
| 469 | + } |
| 470 | +} |
| 471 | + |
| 472 | +func TestSchema_Unmarshal_MergeKeysAtModelLevel_Success(t *testing.T) { |
| 473 | + t.Parallel() |
| 474 | + |
| 475 | + yml := ` |
| 476 | +x-base: &base |
| 477 | + type: object |
| 478 | + description: Base schema description |
| 479 | +<<: *base |
| 480 | +title: Extended Schema |
| 481 | +properties: |
| 482 | + id: |
| 483 | + type: integer |
| 484 | +` |
| 485 | + |
| 486 | + var schema oas3.Schema |
| 487 | + |
| 488 | + validationErrs, err := marshaller.Unmarshal(t.Context(), bytes.NewBufferString(yml), &schema) |
| 489 | + require.NoError(t, err, "unmarshal should succeed") |
| 490 | + require.Empty(t, validationErrs, "should have no validation errors") |
| 491 | + |
| 492 | + types := schema.GetType() |
| 493 | + require.Len(t, types, 1, "should have type from merge") |
| 494 | + assert.Equal(t, oas3.SchemaTypeObject, types[0], "merged type should be object") |
| 495 | + assert.Equal(t, "Base schema description", schema.GetDescription(), "should have description from merge") |
| 496 | + assert.Equal(t, "Extended Schema", schema.GetTitle(), "explicit title should be present") |
| 497 | + |
| 498 | + require.NotNil(t, schema.Properties, "properties should not be nil") |
| 499 | + _, ok := schema.Properties.Get("id") |
| 500 | + assert.True(t, ok, "property 'id' should exist") |
| 501 | +} |
| 502 | + |
| 503 | +func TestSchema_Unmarshal_NestedMergeChain_Success(t *testing.T) { |
| 504 | + t.Parallel() |
| 505 | + |
| 506 | + // base1 defines {id, name}, base2 merges base1 and adds {email}, |
| 507 | + // final schema merges base2 and adds {age}. |
| 508 | + // All four properties should be present via recursive merge resolution. |
| 509 | + yml := ` |
| 510 | +x-base1: &base1 |
| 511 | + id: |
| 512 | + type: integer |
| 513 | + name: |
| 514 | + type: string |
| 515 | +x-base2: &base2 |
| 516 | + <<: *base1 |
| 517 | + email: |
| 518 | + type: string |
| 519 | +type: object |
| 520 | +properties: |
| 521 | + <<: *base2 |
| 522 | + age: |
| 523 | + type: integer |
| 524 | +` |
| 525 | + |
| 526 | + var schema oas3.Schema |
| 527 | + |
| 528 | + validationErrs, err := marshaller.Unmarshal(t.Context(), bytes.NewBufferString(yml), &schema) |
| 529 | + require.NoError(t, err, "unmarshal should succeed") |
| 530 | + require.Empty(t, validationErrs, "should have no validation errors") |
| 531 | + |
| 532 | + require.NotNil(t, schema.Properties, "properties should not be nil") |
| 533 | + |
| 534 | + _, hasMergeKey := schema.Properties.Get("<<") |
| 535 | + assert.False(t, hasMergeKey, "merge key '<<' should not appear as a property name") |
| 536 | + |
| 537 | + expectedProps := []string{"id", "name", "email", "age"} |
| 538 | + for _, prop := range expectedProps { |
| 539 | + propSchema, ok := schema.Properties.Get(prop) |
| 540 | + assert.True(t, ok, "property %q should exist", prop) |
| 541 | + assert.NotNil(t, propSchema, "property %q schema should not be nil", prop) |
| 542 | + } |
| 543 | + |
| 544 | + assert.Equal(t, len(expectedProps), schema.Properties.Len(), "should have all 4 properties from nested merge chain") |
| 545 | +} |
| 546 | + |
| 547 | +func TestSchema_Unmarshal_QuotedLiteralMergeKey_Success(t *testing.T) { |
| 548 | + t.Parallel() |
| 549 | + |
| 550 | + // A quoted '<<' is a regular string key, not a merge key. |
| 551 | + // It should be treated as a normal property name. |
| 552 | + yml := ` |
| 553 | +type: object |
| 554 | +properties: |
| 555 | + '<<': |
| 556 | + type: string |
| 557 | + description: This is a literal property named << |
| 558 | + name: |
| 559 | + type: string |
| 560 | +` |
| 561 | + |
| 562 | + var schema oas3.Schema |
| 563 | + |
| 564 | + validationErrs, err := marshaller.Unmarshal(t.Context(), bytes.NewBufferString(yml), &schema) |
| 565 | + require.NoError(t, err, "unmarshal should succeed") |
| 566 | + require.Empty(t, validationErrs, "should have no validation errors") |
| 567 | + |
| 568 | + require.NotNil(t, schema.Properties, "properties should not be nil") |
| 569 | + assert.Equal(t, 2, schema.Properties.Len(), "should have 2 properties") |
| 570 | + |
| 571 | + propSchema, ok := schema.Properties.Get("<<") |
| 572 | + assert.True(t, ok, "quoted '<<' should be a normal property") |
| 573 | + assert.NotNil(t, propSchema, "quoted '<<' property schema should not be nil") |
| 574 | + |
| 575 | + _, ok = schema.Properties.Get("name") |
| 576 | + assert.True(t, ok, "property 'name' should exist") |
| 577 | +} |
0 commit comments