|
| 1 | +# coding=utf-8 |
| 2 | +"""Unit tests for the formatter module.""" |
| 3 | + |
| 4 | +import yaml |
| 5 | +from click.testing import CliRunner |
| 6 | + |
| 7 | +from generator.cli import cli |
| 8 | +from generator.formatter import format_data_with_schema |
| 9 | + |
| 10 | + |
| 11 | +class SchemaWithRef(dict): |
| 12 | + """A schema dict that carries a $ref, as the generator produces after resolving references.""" |
| 13 | + |
| 14 | + def __init__(self, *args, ref=None, **kwargs): |
| 15 | + super().__init__(*args, **kwargs) |
| 16 | + if ref: |
| 17 | + self.__reference__ = {"$ref": ref} |
| 18 | + |
| 19 | + |
| 20 | +def test_named_array_alias_in_oneof_uses_generated_wrapper(): |
| 21 | + message_schema = SchemaWithRef( |
| 22 | + { |
| 23 | + "type": "object", |
| 24 | + "properties": { |
| 25 | + "role": {"type": "string"}, |
| 26 | + "content": {"type": "string"}, |
| 27 | + }, |
| 28 | + "required": ["role", "content"], |
| 29 | + }, |
| 30 | + ref="#/components/schemas/LLMObsPromptChatMessage", |
| 31 | + ) |
| 32 | + chat_schema = SchemaWithRef( |
| 33 | + { |
| 34 | + "type": "array", |
| 35 | + "items": message_schema, |
| 36 | + "x-generate-alias-as-model": True, |
| 37 | + }, |
| 38 | + ref="#/components/schemas/LLMObsPromptChatTemplate", |
| 39 | + ) |
| 40 | + template_schema = SchemaWithRef( |
| 41 | + {"oneOf": [{"type": "string"}, chat_schema]}, |
| 42 | + ref="#/components/schemas/LLMObsPromptTemplate", |
| 43 | + ) |
| 44 | + |
| 45 | + _, result, imports = format_data_with_schema( |
| 46 | + [ |
| 47 | + {"role": "system", "content": "You help {{company_name}} customers."}, |
| 48 | + {"role": "user", "content": "Answer {{question}}"}, |
| 49 | + ], |
| 50 | + template_schema, |
| 51 | + ) |
| 52 | + |
| 53 | + assert ( |
| 54 | + "new LLMObsPromptTemplate(new LLMObsPromptChatTemplate(" |
| 55 | + "Arrays.asList(" in result |
| 56 | + ) |
| 57 | + assert "new LLMObsPromptTemplate(Arrays.asList(" not in result |
| 58 | + assert "LLMObsPromptChatTemplate" in imports |
| 59 | + |
| 60 | + |
| 61 | +def test_named_array_alias_model_accepts_formatted_list(tmp_path): |
| 62 | + spec_dir = tmp_path / "v2" |
| 63 | + spec_dir.mkdir() |
| 64 | + spec_path = spec_dir / "openapi.yaml" |
| 65 | + spec_path.write_text( |
| 66 | + yaml.safe_dump( |
| 67 | + { |
| 68 | + "openapi": "3.0.0", |
| 69 | + "info": {"title": "test", "version": "1"}, |
| 70 | + "servers": [{"url": "https://api.example.com", "variables": {}}], |
| 71 | + "paths": { |
| 72 | + "/test": { |
| 73 | + "post": { |
| 74 | + "operationId": "TestArrayAlias", |
| 75 | + "tags": ["Test"], |
| 76 | + "requestBody": { |
| 77 | + "content": { |
| 78 | + "application/json": { |
| 79 | + "schema": { |
| 80 | + "$ref": "#/components/schemas/LLMObsPromptChatTemplate" |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + }, |
| 85 | + "responses": {"200": {"description": "OK"}}, |
| 86 | + } |
| 87 | + } |
| 88 | + }, |
| 89 | + "components": { |
| 90 | + "securitySchemes": {}, |
| 91 | + "schemas": { |
| 92 | + "LLMObsPromptChatMessage": { |
| 93 | + "type": "object", |
| 94 | + "properties": { |
| 95 | + "role": {"type": "string"}, |
| 96 | + "content": {"type": "string"}, |
| 97 | + }, |
| 98 | + }, |
| 99 | + "LLMObsPromptChatTemplate": { |
| 100 | + "type": "array", |
| 101 | + "items": {"$ref": "#/components/schemas/LLMObsPromptChatMessage"}, |
| 102 | + "x-generate-alias-as-model": True, |
| 103 | + }, |
| 104 | + } |
| 105 | + }, |
| 106 | + } |
| 107 | + ) |
| 108 | + ) |
| 109 | + output = tmp_path / "generated" |
| 110 | + |
| 111 | + result = CliRunner().invoke(cli, [str(spec_path), "--output", str(output)]) |
| 112 | + |
| 113 | + assert result.exit_code == 0 |
| 114 | + model = (output / "v2/model/LLMObsPromptChatTemplate.java").read_text() |
| 115 | + assert "public LLMObsPromptChatTemplate() {}" in model |
| 116 | + assert "public LLMObsPromptChatTemplate(List<LLMObsPromptChatMessage> items)" in model |
| 117 | + assert "super(items);" in model |
0 commit comments