Skip to content

Commit 001d8f5

Browse files
committed
Update invalidSchema action with support for all Dev Proxy schemas. Closes #168
Closes #168
1 parent a14fdaa commit 001d8f5

3 files changed

Lines changed: 84 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
- Snippets: All snippets that reference schemas updated to use `v2.1.0` schema
2828
- Diagnostics: Improved highlighting to target specific nodes instead of entire objects
2929
- Diagnostics: All diagnostics now use unique codes for better identification
30+
- Quick Fixes: Update schema action now supports all Dev Proxy file schemas, not just config files
3031

3132
### Fixed:
3233

src/code-actions.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ function registerInvalidSchemaFixes(
8585
return;
8686
}
8787

88+
// Extract the schema filename from the current schema URL
89+
const currentSchemaText = document.getText(diagnostic.range);
90+
const schemaFilename = extractSchemaFilename(currentSchemaText);
91+
8892
const fix = new vscode.CodeAction(
8993
'Update schema',
9094
vscode.CodeActionKind.QuickFix,
@@ -93,7 +97,7 @@ function registerInvalidSchemaFixes(
9397
fix.edit.replace(
9498
document.uri,
9599
diagnostic.range,
96-
`https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v${devProxyVersion}/rc.schema.json`,
100+
`https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v${devProxyVersion}/${schemaFilename}`,
97101
);
98102
fix.isPreferred = true;
99103
return [fix];
@@ -103,6 +107,27 @@ function registerInvalidSchemaFixes(
103107
registerJsonCodeActionProvider(context, invalidSchema);
104108
}
105109

110+
/**
111+
* Extract the schema filename from a schema URL.
112+
* Returns the filename (e.g., 'mockresponseplugin.mocksfile.schema.json')
113+
* or defaults to 'rc.schema.json' if extraction fails.
114+
*/
115+
export function extractSchemaFilename(schemaUrl: string): string {
116+
const defaultSchema = 'rc.schema.json';
117+
118+
try {
119+
// Match any .schema.json filename at the end of the URL
120+
const match = schemaUrl.match(/([^/]+\.schema\.json)/i);
121+
if (match) {
122+
return match[1];
123+
}
124+
} catch {
125+
// Fall through to default
126+
}
127+
128+
return defaultSchema;
129+
}
130+
106131
function registerDeprecatedPluginPathFixes(context: vscode.ExtensionContext) {
107132
const correctPluginPath = '~appFolder/plugins/DevProxy.Plugins.dll';
108133

src/test/code-actions.test.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import * as assert from 'assert';
66
import * as sinon from 'sinon';
77
import * as vscode from 'vscode';
8-
import { registerCodeActions } from '../code-actions';
8+
import { registerCodeActions, extractSchemaFilename } from '../code-actions';
99
import { getExtensionContext, createDevProxyInstall, getFixturePath } from './helpers';
1010
import { DiagnosticCodes } from '../constants';
1111
import { getDiagnosticCode, sleep } from '../utils';
@@ -533,3 +533,59 @@ suite('Code Action Provider Registration', () => {
533533
registerCodeActions(contextWithInstall);
534534
});
535535
});
536+
537+
suite('extractSchemaFilename', () => {
538+
test('should extract rc.schema.json from config file schema URL', () => {
539+
const url = 'https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.0/rc.schema.json';
540+
assert.strictEqual(extractSchemaFilename(url), 'rc.schema.json');
541+
});
542+
543+
test('should extract mockresponseplugin.mocksfile.schema.json from mocks file schema URL', () => {
544+
const url = 'https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/mockresponseplugin.mocksfile.schema.json';
545+
assert.strictEqual(extractSchemaFilename(url), 'mockresponseplugin.mocksfile.schema.json');
546+
});
547+
548+
test('should extract crudapiplugin.apifile.schema.json from CRUD API file schema URL', () => {
549+
const url = 'https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.1.0/crudapiplugin.apifile.schema.json';
550+
assert.strictEqual(extractSchemaFilename(url), 'crudapiplugin.apifile.schema.json');
551+
});
552+
553+
test('should extract rewriteplugin.rewritesfile.schema.json from rewrites file schema URL', () => {
554+
const url = 'https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.25.0/rewriteplugin.rewritesfile.schema.json';
555+
assert.strictEqual(extractSchemaFilename(url), 'rewriteplugin.rewritesfile.schema.json');
556+
});
557+
558+
test('should extract genericrandomerrorplugin.errorsfile.schema.json', () => {
559+
const url = 'https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.0.0/genericrandomerrorplugin.errorsfile.schema.json';
560+
assert.strictEqual(extractSchemaFilename(url), 'genericrandomerrorplugin.errorsfile.schema.json');
561+
});
562+
563+
test('should extract ratelimitingplugin.customresponsefile.schema.json', () => {
564+
const url = 'https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/ratelimitingplugin.customresponsefile.schema.json';
565+
assert.strictEqual(extractSchemaFilename(url), 'ratelimitingplugin.customresponsefile.schema.json');
566+
});
567+
568+
test('should return default rc.schema.json for URL without .schema.json', () => {
569+
const url = 'https://example.com/some/path/file.json';
570+
assert.strictEqual(extractSchemaFilename(url), 'rc.schema.json');
571+
});
572+
573+
test('should return default rc.schema.json for empty string', () => {
574+
assert.strictEqual(extractSchemaFilename(''), 'rc.schema.json');
575+
});
576+
577+
test('should return default rc.schema.json for malformed URL', () => {
578+
const url = 'not-a-valid-url';
579+
assert.strictEqual(extractSchemaFilename(url), 'rc.schema.json');
580+
});
581+
582+
test('should handle schema URL from old microsoft org', () => {
583+
const url = 'https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.20.0/mockresponseplugin.schema.json';
584+
assert.strictEqual(extractSchemaFilename(url), 'mockresponseplugin.schema.json');
585+
});
586+
587+
test('should be case-insensitive for .schema.json extension', () => {
588+
const url = 'https://example.com/path/MyPlugin.SCHEMA.JSON';
589+
assert.strictEqual(extractSchemaFilename(url), 'MyPlugin.SCHEMA.JSON');
590+
});
591+
});

0 commit comments

Comments
 (0)