REST APIs, based on the REpresentational State Transfer (REST) architecture, are the primary type of Web API. The OpenAPI Specification (OAS) serves as the de facto standard for describing REST APIs and is crucial for multiple software engineering tasks. However, developers face challenges in writing and maintaining OAS. Although static analysis shows potential for OAS generation, it is limited to specific programming languages and development frameworks. The powerful code understanding capabilities of LLMs offer new opportunities for OAS generation, yet they are constrained by context limitations and hallucinations. To address these challenges, we propose the OpenAI OpenAPI Project Scanner (OOPS), the first technology-agnostic LLM-based static analysis method for OAS generation, requiring fewer technology-specific rules and less human expert intervention. OOPS is implemented as an LLM agent workflow comprising two key steps: endpoint method extraction and OAS generation. By constructing an API dependency graph, it establishes necessary file associations to address LLMs' context limitations. Through multi-stage generation and self-refine, it mitigates both syntactic and semantic hallucinations during OAS generation. We evaluated OOPS on 12 real-world REST APIs spanning 5 programming languages and 8 development frameworks. Experimental results demonstrate that OOPS accurately generates high-quality OAS for REST APIs implemented with diverse technologies, achieving an average F1-score exceeding 98% for endpoint method inference, 97% for both request parameter and response inference, and 92% for parameter constraint inference. The input tokens average below 5.6K with a maximum of 16.2K, while the output tokens average below 0.9K with a maximum of 7.7K.
翻译:基于表述性状态转移(REST)架构的REST API是Web API的主要类型。OpenAPI规范(OAS)作为描述REST API的事实标准,对多项软件工程任务至关重要。然而,开发者在编写和维护OAS方面面临挑战。尽管静态分析在OAS生成方面展现出潜力,但其仅限于特定的编程语言和开发框架。大语言模型强大的代码理解能力为OAS生成提供了新机遇,但仍受限于上下文约束与幻觉问题。为应对这些挑战,我们提出了OpenAI OpenAPI项目扫描器(OOPS),这是首个与技术栈无关、基于大语言模型的OAS生成静态分析方法,该方法需要更少的技术特定规则和人工专家干预。OOPS被实现为一个包含两个关键步骤的大语言模型智能体工作流:端点方法提取与OAS生成。通过构建API依赖图,该方法建立了必要的文件关联以应对大语言模型的上下文限制。通过多阶段生成与自我优化机制,有效缓解了OAS生成过程中的语法与语义幻觉问题。我们在涵盖5种编程语言和8种开发框架的12个真实REST API上对OOPS进行了评估。实验结果表明,OOPS能够为采用不同技术实现的REST API准确生成高质量的OAS,在端点方法推断方面平均F1分数超过98%,请求参数与响应推断均达到97%,参数约束推断达到92%。输入令牌数平均低于5.6K(最高16.2K),输出令牌数平均低于0.9K(最高7.7K)。