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

examples-automation, map example JSON from typespec folder to swagger folder #8533

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion tools/azure-rest-api-specs-examples-automation/java/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import argparse
import logging
import dataclasses
import re
import glob
from typing import List

from modules import JavaExample, JavaFormatResult
Expand All @@ -14,6 +16,7 @@

script_path: str = '.'
tmp_path: str
specs_path: str

namespace = 'com.azure.resourcemanager'

Expand Down Expand Up @@ -172,6 +175,8 @@ def process_java_example_content(lines: List[str], class_name: str) -> List[Java
example_filepath = java_example_method.example_relative_path
example_dir, example_filename = path.split(example_filepath)

example_dir = try_find_resource_manager_example(example_dir, example_filename)

# use Main as class name
old_class_name = class_name
new_class_name = 'Main'
Expand All @@ -188,6 +193,39 @@ def process_java_example_content(lines: List[str], class_name: str) -> List[Java
return java_examples


def set_specs_path(new_specs_path: str):
# for test
global specs_path
specs_path = new_specs_path


def try_find_resource_manager_example(example_dir: str, example_filename: str) -> str:
if '/resource-manager/' not in example_dir:
try:
match = re.match(r'specification/([^/]+)/.*/examples/([^/]+)(.*)', example_dir)
if match:
# example: specification/mongocluster/DocumentDB.MongoCluster.Management/examples/2024-03-01-preview
# service: mongocluster
# api_version: 2024-03-01-preview
# additional_path: <empty>

service = match.group(1)
api_version = match.group(2)
additional_path = match.group(3)

glob_resource_manager_filename = f'specification/{service}/resource-manager/**/{api_version}/examples{additional_path}/{example_filename}'
candidate_resource_manager_filename = glob.glob(path.join(specs_path, glob_resource_manager_filename),
recursive=True)
if len(candidate_resource_manager_filename) > 0:
example_path, _ = path.split(candidate_resource_manager_filename[0])
example_dir = path.relpath(example_path, specs_path).replace('\\', '/')
except NameError:
# specs_path not defined
pass

return example_dir


def validate_java_examples(release: Release, java_examples: List[JavaExample]) -> JavaFormatResult:
# batch validate Java examples

Expand Down Expand Up @@ -275,6 +313,7 @@ def create_java_examples(release: Release, sdk_examples_path: str, java_examples
def main():
global script_path
global tmp_path
global specs_path

logging.basicConfig(level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s',
Expand All @@ -291,7 +330,7 @@ def main():
with open(input_json_path, 'r', encoding='utf-8') as f_in:
config = json.load(f_in)

# specs_path = config['specsPath']
specs_path = config['specsPath']
sdk_path = config['sdkPath']
sdk_examples_path = config['sdkExamplesPath']
tmp_path = config['tempPath']
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest

from main import process_java_example_content
from main import set_specs_path


class TestProcess(unittest.TestCase):
Expand Down Expand Up @@ -252,3 +253,41 @@ def test_process_java_example_method_signature_newline(self):

self.assertEqual('specification/compute/resource-manager/Microsoft.Compute/ComputeRP/stable/2024-03-01/examples-java/virtualMachineScaleSetExamples', java_examples[1].target_dir)
self.assertEqual('VirtualMachineScaleSetVM_Start_MinimumSet_Gen', java_examples[1].target_filename)

@unittest.skip('require call "set_specs_path" with your local azure-rest-api-specs repository')
def test_process_java_example_original_file_typespec(self):
java_code = '''
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) TypeSpec Code Generator.

package com.azure.resourcemanager.mongocluster.generated;

/**
* Samples for MongoClusters ListConnectionStrings.
*/
public final class MongoClustersListConnectionStringsSamples {
/*
* x-ms-original-file: specification/mongocluster/DocumentDB.MongoCluster.Management/examples/2024-03-01-preview/
* MongoClusters_ListConnectionStrings.json
*/
/**
* Sample code: List the available connection strings for the Mongo Cluster resource.
*
* @param manager Entry point to MongoClusterManager.
*/
public static void listTheAvailableConnectionStringsForTheMongoClusterResource(
com.azure.resourcemanager.mongocluster.MongoClusterManager manager) {
manager.mongoClusters()
.listConnectionStringsWithResponse("TestGroup", "myMongoCluster", com.azure.core.util.Context.NONE);
}
}
'''

set_specs_path('c:/github/azure-rest-api-specs')
java_examples = process_java_example_content(java_code.splitlines(keepends=True),
'MongoClustersListConnectionStringsSamples')

self.assertEqual(1, len(java_examples))

self.assertEqual('specification/mongocluster/resource-manager/Microsoft.DocumentDB/preview/2024-03-01-preview/examples-java', java_examples[0].target_dir)