Revert "Add validator to prevent discriminator and encodedname used together" (#3852)
Reverts microsoft/typespec#3763 This is actually a pattern used for things like `@odata.kind` and would need more discussion to disallow
This commit is contained in:
Родитель
0442e27399
Коммит
27ed13de58
|
@ -1,8 +0,0 @@
|
|||
---
|
||||
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
|
||||
changeKind: feature
|
||||
packages:
|
||||
- "@typespec/compiler"
|
||||
---
|
||||
|
||||
Add validator to prevent discriminator and encodedname used together
|
|
@ -35,7 +35,8 @@ jobs:
|
|||
if: |
|
||||
!startsWith(github.head_ref, 'publish/') &&
|
||||
!startsWith(github.head_ref, 'dependabot/') &&
|
||||
!startsWith(github.head_ref, 'backmerge/')
|
||||
!startsWith(github.head_ref, 'backmerge/') &&
|
||||
!startsWith(github.head_ref, 'revert-')
|
||||
|
||||
# Validate spell check
|
||||
spellcheck:
|
||||
|
|
|
@ -86,7 +86,6 @@ In this example, the `Child` model overrides the `example` property from the `Ba
|
|||
### @typespec/compiler
|
||||
|
||||
- [#3699](https://github.com/microsoft/typespec/pull/3699) Moved compiler dependencies to peer and dev for scaffolded projects.
|
||||
- [#3763](https://github.com/microsoft/typespec/pull/3763) Add validator to prevent discriminator and encodedname used together
|
||||
- [59daf90](https://github.com/microsoft/typespec/commit/59daf9054c709d3657e498b5f6dbe77492c98210) Add new `@example` and `@opExample` decorator to provide examples on types and operations.
|
||||
|
||||
```tsp
|
||||
|
|
|
@ -876,12 +876,6 @@ const diagnostics = {
|
|||
duplicate: paramMessage`Same encoded name '${"name"}' is used for 2 members '${"mimeType"}'`,
|
||||
},
|
||||
},
|
||||
"discriminator-encodedname": {
|
||||
severity: "error",
|
||||
messages: {
|
||||
default: paramMessage`Discriminator property '${"discriminator"}' cannot be use an different encoded name.`,
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Service
|
||||
|
|
|
@ -69,7 +69,7 @@ import {
|
|||
TypeSpecLibrary,
|
||||
TypeSpecScriptNode,
|
||||
} from "./types.js";
|
||||
import { validateIncompatibleEncodedNameAndDiscriminator } from "./validators/encoded-name-discriminator.js";
|
||||
|
||||
export interface ProjectedProgram extends Program {
|
||||
projector: Projector;
|
||||
}
|
||||
|
@ -742,7 +742,6 @@ export async function compile(
|
|||
/** Run the compiler built-in validators */
|
||||
function runCompilerValidators() {
|
||||
validateEncodedNamesConflicts(program);
|
||||
validateIncompatibleEncodedNameAndDiscriminator(program);
|
||||
}
|
||||
|
||||
function validateRequiredImports() {
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
import { getEncodedNames } from "../../lib/encoded-names.js";
|
||||
import { Discriminator, getDiscriminatedTypes } from "../intrinsic-type-state.js";
|
||||
import { reportDiagnostic } from "../messages.js";
|
||||
import type { Program } from "../program.js";
|
||||
import { Model } from "../types.js";
|
||||
|
||||
/**
|
||||
* Validate you do not use `@encodedName` on a discriminator property.
|
||||
*/
|
||||
export function validateIncompatibleEncodedNameAndDiscriminator(program: Program) {
|
||||
for (const [type, discriminator] of getDiscriminatedTypes(program)) {
|
||||
if (type.kind === "Model") {
|
||||
validateModel(program, type, discriminator);
|
||||
for (const child of type.derivedModels) {
|
||||
validateModel(program, child, discriminator);
|
||||
}
|
||||
} else {
|
||||
for (const variant of type.variants.values()) {
|
||||
if (variant.type.kind === "Model") {
|
||||
validateModel(program, variant.type, discriminator);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function validateModel(program: Program, type: Model, discriminator: Discriminator) {
|
||||
const property = type.properties.get(discriminator.propertyName);
|
||||
const names = property && getEncodedNames(program, property);
|
||||
if (names && names?.size > 0) {
|
||||
reportDiagnostic(program, {
|
||||
code: "discriminator-encodedname",
|
||||
target: type,
|
||||
format: { discriminator: discriminator.propertyName },
|
||||
});
|
||||
}
|
||||
}
|
|
@ -39,13 +39,6 @@ export function $encodedName(
|
|||
existing.set(mimeType, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export function getEncodedNames(program: Program, target: Type): Map<string, string> | undefined {
|
||||
return program.stateMap(encodedNameKey).get(target);
|
||||
}
|
||||
|
||||
function getEncodedName(program: Program, target: Type, mimeType: string): string | undefined {
|
||||
const mimeTypeObj = parseMimeType(mimeType);
|
||||
if (mimeTypeObj === undefined) {
|
||||
|
@ -54,7 +47,7 @@ function getEncodedName(program: Program, target: Type, mimeType: string): strin
|
|||
const resolvedMimeType = mimeTypeObj?.suffix
|
||||
? `${mimeTypeObj.type}/${mimeTypeObj.suffix}`
|
||||
: mimeType;
|
||||
return getEncodedNames(program, target)?.get(resolvedMimeType);
|
||||
return program.stateMap(encodedNameKey).get(target)?.get(resolvedMimeType);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1319,58 +1319,6 @@ describe("compiler: built-in decorators", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("error when use on discriminator", () => {
|
||||
it("emit error when use on base property", async () => {
|
||||
const diagnostics = await runner.diagnose(`
|
||||
@discriminator("kind")
|
||||
model Cert {
|
||||
@encodedName("application/json", "k")
|
||||
kind: string;
|
||||
}
|
||||
`);
|
||||
|
||||
expectDiagnostics(diagnostics, {
|
||||
code: "discriminator-encodedname",
|
||||
});
|
||||
});
|
||||
|
||||
it("emit error when use on derived model property", async () => {
|
||||
const diagnostics = await runner.diagnose(`
|
||||
@discriminator("kind")
|
||||
model Base {
|
||||
kind: string;
|
||||
}
|
||||
|
||||
model Child extends Base {
|
||||
@encodedName("application/json", "k")
|
||||
kind: "child";
|
||||
}
|
||||
`);
|
||||
|
||||
expectDiagnostics(diagnostics, {
|
||||
code: "discriminator-encodedname",
|
||||
});
|
||||
});
|
||||
|
||||
it("emit error when use on union variant property", async () => {
|
||||
const diagnostics = await runner.diagnose(`
|
||||
@discriminator("kind")
|
||||
union Base {
|
||||
child: Child
|
||||
}
|
||||
|
||||
model Child {
|
||||
@encodedName("application/json", "k")
|
||||
kind: "child";
|
||||
}
|
||||
`);
|
||||
|
||||
expectDiagnostics(diagnostics, {
|
||||
code: "discriminator-encodedname",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("resolve explicit encoded name", async () => {
|
||||
const { expireAt } = (await runner.compile(`
|
||||
model Cert {
|
||||
|
|
Загрузка…
Ссылка в новой задаче