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

Add xml support to openapi3 #4214

Merged
merged 156 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
156 commits
Select commit Hold shift + click to select a range
324231d
initial xml schema
Aug 20, 2024
fb8d16a
update
Aug 20, 2024
fd480f1
Merge branch 'main' into xmlClient
skywing918 Aug 20, 2024
430f1c2
update all logic for xml object
Aug 23, 2024
0b1d92e
Merge branch 'main' into xmlClient
skywing918 Aug 23, 2024
76d78bb
update
Aug 23, 2024
979080f
Merge branch 'xmlClient' of https://github.com/skywing918/typespec in…
Aug 23, 2024
afcc306
update cases
Aug 23, 2024
7f7a8c0
Merge branch 'microsoft:main' into xmlClient
skywing918 Aug 26, 2024
6bc86c6
Merge branch 'main' into xmlClient
skywing918 Aug 27, 2024
c55136d
update Scenario 1.3
Aug 27, 2024
d2e97f3
Merge branch 'main' into xmlClient
skywing918 Aug 27, 2024
ccbfaee
Merge branch 'main' into xmlClient
skywing918 Aug 29, 2024
06f9fdc
Merge branch 'main' into xmlClient
skywing918 Aug 30, 2024
4b99815
move func from compiler to openapi3
Aug 30, 2024
56ccc79
update for comments
Aug 30, 2024
4079c23
revert
Aug 30, 2024
2b27393
update
Aug 30, 2024
dabf28e
Merge branch 'main' into xmlClient
skywing918 Sep 2, 2024
93cddcb
update for comments
Sep 2, 2024
0d26bea
update
Sep 2, 2024
9985914
up
Sep 2, 2024
a490317
Merge branch 'main' into xmlClient
skywing918 Sep 3, 2024
63cfee9
up
Sep 3, 2024
0b7ea1d
fix e2e
Sep 3, 2024
fc62a47
update
Sep 3, 2024
5bb5d4d
Merge branch 'main' into xmlClient
skywing918 Sep 3, 2024
989ff61
Merge branch 'main' into xmlClient
skywing918 Sep 4, 2024
5e1fc0e
keep @typespec/xml in peer dependency
Sep 4, 2024
de24d61
Merge branch 'main' into xmlClient
skywing918 Sep 5, 2024
d2893b1
up for comments
Sep 5, 2024
4be3f23
Merge branch 'main' into xmlClient
skywing918 Sep 5, 2024
6270e47
update
Sep 5, 2024
84dc12c
up
Sep 5, 2024
7ef1127
Merge branch 'main' into xmlClient
skywing918 Sep 6, 2024
9555956
update
Sep 6, 2024
175f9d9
Merge branch 'main' into xmlClient
skywing918 Sep 6, 2024
0301b1f
up
Sep 6, 2024
86a686a
Update .chronus/changes/simple-xml-support-2024-7-23-14-23-13.md
skywing918 Sep 9, 2024
ae5a0c7
Merge branch 'main' into xmlClient
skywing918 Sep 9, 2024
d2682a6
Update packages/openapi3/test/xml-models.test.ts
skywing918 Sep 9, 2024
626a566
Update packages/openapi3/test/xml-models.test.ts
skywing918 Sep 9, 2024
7d16724
Update packages/openapi3/test/xml-models.test.ts
skywing918 Sep 9, 2024
dd08c12
Update packages/openapi3/test/xml-models.test.ts
skywing918 Sep 9, 2024
c51acdd
update
Sep 9, 2024
a075e29
update
Sep 9, 2024
a726323
up cases
Sep 9, 2024
3c47cd2
Merge branch 'main' into xmlClient
skywing918 Sep 10, 2024
b247bc6
revert e2e test
Sep 10, 2024
d57a7e4
update
Sep 10, 2024
648cba4
up
Sep 10, 2024
4305fd4
Merge branch 'main' into xmlClient
skywing918 Sep 10, 2024
6286084
update
Sep 10, 2024
2e6fc01
up
Sep 10, 2024
7616f43
Merge branch 'main' into xmlClient
skywing918 Sep 10, 2024
bb59a5d
Merge branch 'main' into xmlClient
skywing918 Sep 10, 2024
189562f
Merge branch 'main' into xmlClient
skywing918 Sep 11, 2024
0fb93e8
add warning logic
Sep 11, 2024
8d765ce
Merge branch 'main' into xmlClient
skywing918 Sep 11, 2024
596d8e1
Merge branch 'main' into xmlClient
skywing918 Sep 12, 2024
c3a6f83
update
Sep 12, 2024
da514d6
update for comments
Sep 12, 2024
1341213
Merge branch 'main' into xmlClient
skywing918 Sep 12, 2024
6478392
Merge branch 'main' into xmlClient
skywing918 Sep 13, 2024
c3c1d9e
wrap the items in allOf if a model
Sep 13, 2024
cbd8840
update scalar logic in items
Sep 13, 2024
ad29aa4
Merge branch 'main' into xmlClient
skywing918 Sep 14, 2024
16fdc8b
merged
Sep 14, 2024
a3b6248
Merge branch 'main' into xmlClient
skywing918 Sep 18, 2024
aa815a7
update formatting
Sep 18, 2024
61ebd49
Merge branch 'main' into xmlClient
skywing918 Sep 19, 2024
22f8db1
Merge branch 'main' into xmlClient
skywing918 Sep 20, 2024
90f73d4
Merge branch 'main' into xmlClient
skywing918 Sep 20, 2024
e9c1b63
Merge branch 'main' into xmlClient
skywing918 Sep 23, 2024
5e9391c
Merge branch 'main' into xmlClient
skywing918 Sep 24, 2024
98aebc0
update
Sep 24, 2024
3d2eb86
update
Sep 24, 2024
b0524d9
update cases
Sep 24, 2024
dc8171d
Merge branch 'main' into xmlClient
skywing918 Sep 24, 2024
e681cc8
Merge branch 'main' into xmlClient
skywing918 Sep 25, 2024
6178967
Merge branch 'main' into xmlClient
skywing918 Sep 26, 2024
456ad6c
Merge branch 'main' into xmlClient
skywing918 Sep 26, 2024
05e399d
Merge branch 'main' into xmlClient
skywing918 Sep 27, 2024
8437312
Merge branch 'main' into xmlClient
skywing918 Sep 27, 2024
fde8c7d
Update packages/openapi3/src/lib.ts
skywing918 Sep 27, 2024
7034dbf
Merge branch 'microsoft:main' into xmlClient
skywing918 Sep 27, 2024
579bdfb
update for comments
Sep 27, 2024
2f1fbf5
update doc
Sep 27, 2024
5d69f5e
Merge branch 'main' into xmlClient
skywing918 Sep 27, 2024
01d329e
up
Sep 27, 2024
7490fbb
Merge branch 'main' into xmlClient
skywing918 Sep 28, 2024
c37bb36
Merge branch 'main' into xmlClient
skywing918 Sep 29, 2024
5f49cf1
the xml name needs to be set if that object is used in an xml payload
Sep 29, 2024
fb2fbae
circular reference cases
Sep 29, 2024
3570c22
up
Sep 29, 2024
cfdb9ed
Merge branch 'main' into xmlClient
skywing918 Sep 29, 2024
542c70c
Merge branch 'main' into xmlClient
skywing918 Sep 30, 2024
eafd40c
set xml name in items if that scalar object is used in an xml payload.
Sep 30, 2024
0549f42
revert scalar logic on array
Sep 30, 2024
20dedef
fix
Sep 30, 2024
b069491
update cases
Sep 30, 2024
76b8c67
Merge branch 'main' into xmlClient
skywing918 Sep 30, 2024
ddf0690
Merge branch 'main' into xmlClient
skywing918 Sep 30, 2024
7011b9d
Merge branch 'main' into xmlClient
skywing918 Oct 1, 2024
7dd4aa6
Merge branch 'main' into xmlClient
skywing918 Oct 2, 2024
54c2bea
Merge branch 'main' into xmlClient
skywing918 Oct 8, 2024
45f4434
Merge branch 'main' into xmlClient
skywing918 Oct 10, 2024
8a1da01
Merge branch 'main' into xmlClient
skywing918 Oct 10, 2024
5b5488c
Merge branch 'main' into xmlClient
skywing918 Oct 11, 2024
95cc03c
Merge branch 'main' into xmlClient
skywing918 Oct 11, 2024
e766d4a
Merge branch 'microsoft:main' into xmlClient
skywing918 Oct 12, 2024
525fbf2
update for comments
Oct 12, 2024
add0160
update cases
Oct 12, 2024
4b80e19
unwrapped use property name
Oct 12, 2024
43cbe53
fix build
Oct 12, 2024
e753cee
Merge branch 'main' into xmlClient
skywing918 Oct 14, 2024
ecae480
update with dynamic loading
Oct 14, 2024
2511ef2
remove @typespec/xml from peerDependencies
Oct 14, 2024
99e06d7
remove unless
Oct 14, 2024
0940808
Merge branch 'main' into xmlClient
skywing918 Oct 15, 2024
4a9c32d
update for comments
Oct 15, 2024
c9ce78d
Merge branch 'main' into xmlClient
skywing918 Oct 15, 2024
fdff71f
remove b.array
Oct 15, 2024
89315d9
try import
Oct 15, 2024
39fce11
Merge branch 'main' into xmlClient
skywing918 Oct 16, 2024
a94c1de
Merge branch 'main' into xmlClient
skywing918 Oct 16, 2024
f361191
Update packages/openapi3/src/lib.ts
skywing918 Oct 17, 2024
2bb718d
Update packages/openapi3/src/lib.ts
skywing918 Oct 17, 2024
a21eeac
Update packages/openapi3/src/xml-module.ts
skywing918 Oct 17, 2024
c57a917
Update packages/openapi3/src/xml-module.ts
skywing918 Oct 17, 2024
2395995
update
Oct 17, 2024
70c9226
Merge branch 'main' into xmlClient
skywing918 Oct 17, 2024
3e31d9a
update case
Oct 17, 2024
e702666
change the type of ref be OpenAPI3Schema
Oct 17, 2024
622e660
warning and change the type to be type: string if attribute is object
Oct 17, 2024
b4f5eb2
Merge branch 'main' into xmlClient
skywing918 Oct 17, 2024
0e7d771
update
Oct 17, 2024
13b7be2
update message
Oct 17, 2024
8cb031e
Merge branch 'main' into xmlClient
skywing918 Oct 17, 2024
b45816b
Update packages/openapi3/src/lib.ts
skywing918 Oct 17, 2024
7e55f1f
Update packages/openapi3/src/lib.ts
skywing918 Oct 17, 2024
c477090
Merge branch 'main' into xmlClient
skywing918 Oct 17, 2024
66249f1
update case
Oct 18, 2024
f678268
Merge branch 'main' into xmlClient
skywing918 Oct 18, 2024
71a5b13
Merge branch 'main' into xmlClient
skywing918 Oct 19, 2024
d0475db
Merge branch 'main' into xmlClient
skywing918 Oct 21, 2024
9b09eba
Merge branch 'main' into xmlClient
skywing918 Oct 22, 2024
a66e099
Merge branch 'main' into xmlClient
skywing918 Oct 23, 2024
903854b
Merge branch 'main' into xmlClient
skywing918 Oct 24, 2024
cbd792c
update
Oct 24, 2024
1183f81
update for comments
Oct 24, 2024
1a87c4b
update
Oct 24, 2024
1be9810
Merge branch 'main' into xmlClient
skywing918 Oct 25, 2024
257bf70
Merge branch 'main' into xmlClient
skywing918 Oct 28, 2024
eee70ea
update
Oct 28, 2024
164d269
update
Oct 28, 2024
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
8 changes: 8 additions & 0 deletions .chronus/changes/simple-xml-support-2024-7-23-14-23-13.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
changeKind: feature
packages:
- "@typespec/compiler"
skywing918 marked this conversation as resolved.
Show resolved Hide resolved
- "@typespec/openapi3"
---

Add xml support to openapi3
3 changes: 3 additions & 0 deletions packages/compiler/src/core/index.ts
skywing918 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export {
getMinValueAsNumeric,
getMinValueExclusive,
getMinValueExclusiveAsNumeric,
getXmlNs,
isXmlAttribute,
isXmlUnwrapped,
type Discriminator,
} from "./intrinsic-type-state.js";
export {
Expand Down
26 changes: 26 additions & 0 deletions packages/compiler/src/core/intrinsic-type-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function createStateSymbol(name: string) {
return Symbol.for(`TypeSpec.${name}`);
}

function createXmlStateSymbol(name: string) {
return Symbol.for(`@typespec/xml/${name}`);
}

const stateKeys = {
minValues: createStateSymbol("minValues"),
maxValues: createStateSymbol("maxValues"),
Expand All @@ -26,6 +30,12 @@ const stateKeys = {
discriminator: createStateSymbol("discriminator"),
};

const XmlStateKeys = {
attribute: createXmlStateSymbol("attribute"),
unwrapped: createXmlStateSymbol("unwrapped"),
nsDeclaration: createXmlStateSymbol("nsDeclaration"),
};

// #region @minValue

export function setMinValue(program: Program, target: Type, value: Numeric): void {
Expand Down Expand Up @@ -150,6 +160,22 @@ export function getMaxItems(program: Program, target: Type): number | undefined
}
// #endregion @maxItems

// #region @xml object
export function isXmlAttribute(program: Program, target: Type): boolean {
return program.stateSet(XmlStateKeys.attribute).has(target);
}
export function isXmlUnwrapped(program: Program, target: Type): boolean {
return program.stateSet(XmlStateKeys.unwrapped).has(target);
}
export function getXmlNs(program: Program, target: Type): XmlNamespace | undefined {
return program.stateMap(XmlStateKeys.nsDeclaration).get(target);
}
export interface XmlNamespace {
readonly namespace: string;
readonly prefix: string;
}
// #endregion @xml object

// #region doc

/** @internal */
Expand Down
4 changes: 3 additions & 1 deletion packages/openapi3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"@typespec/compiler": "workspace:~",
"@typespec/http": "workspace:~",
"@typespec/openapi": "workspace:~",
"@typespec/versioning": "workspace:~"
"@typespec/versioning": "workspace:~",
"@typespec/xml": "workspace:~"
skywing918 marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@types/node": "~18.11.19",
Expand All @@ -76,6 +77,7 @@
"@typespec/rest": "workspace:~",
"@typespec/tspd": "workspace:~",
"@typespec/versioning": "workspace:~",
"@typespec/xml": "workspace:~",
"@vitest/coverage-v8": "^2.0.4",
"@vitest/ui": "^2.0.4",
"c8": "^10.1.2",
Expand Down
56 changes: 55 additions & 1 deletion packages/openapi3/src/schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ import {
getPattern,
getSummary,
getTypeName,
getXmlNs,
ignoreDiagnostics,
isArrayModelType,
isNeverType,
isNullType,
isSecret,
isTemplateDeclaration,
isXmlAttribute,
isXmlUnwrapped,
resolveEncodedName,
serializeValueAsJson,
} from "@typespec/compiler";
Expand Down Expand Up @@ -77,7 +80,12 @@ import { applyEncoding } from "./encoding.js";
import { OpenAPI3EmitterOptions, reportDiagnostic } from "./lib.js";
import { ResolvedOpenAPI3EmitterOptions } from "./openapi.js";
import { getSchemaForStdScalars } from "./std-scalar-schemas.js";
import { OpenAPI3Discriminator, OpenAPI3Schema, OpenAPI3SchemaProperty } from "./types.js";
import {
OpenAPI3Discriminator,
OpenAPI3Schema,
OpenAPI3SchemaProperty,
OpenAPI3XmlSchema,
} from "./types.js";
import { VisibilityUsageTracker } from "./visibility-usage.js";

/**
Expand Down Expand Up @@ -645,6 +653,50 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
}
}

#attachXmlObject(program: Program, prop: ModelProperty, emitObject: OpenAPI3Schema) {
skywing918 marked this conversation as resolved.
Show resolved Hide resolved
// Attach xml object
const schema = new ObjectBuilder({});

// Utility function to apply constraints
const applyConstraint = (fn: (p: Program, t: Type) => any, key: keyof OpenAPI3XmlSchema) => {
skywing918 marked this conversation as resolved.
Show resolved Hide resolved
const value = fn(program, prop);
if (value !== undefined && value !== false) {
skywing918 marked this conversation as resolved.
Show resolved Hide resolved
schema[key] = value;
}
};

// Resolve XML name
const xmlName = resolveEncodedName(program, prop, "application/xml");

if (xmlName !== prop.name) {
skywing918 marked this conversation as resolved.
Show resolved Hide resolved
skywing918 marked this conversation as resolved.
Show resolved Hide resolved
schema.name = xmlName;
}

// Get and set XML namespace if present
const currNs = getXmlNs(program, prop);
if (currNs) {
schema.prefix = currNs.prefix;
schema.namespace = currNs.namespace;
}

applyConstraint(isXmlAttribute, "attribute");

// Handle array wrapping if necessary
skywing918 marked this conversation as resolved.
Show resolved Hide resolved
if (
prop.type?.kind === "Model" &&
isArrayModelType(program, prop.type) &&
xmlName !== prop.name &&
wanlwanl marked this conversation as resolved.
Show resolved Hide resolved
!isXmlUnwrapped(program, prop)
) {
schema.wrapped = true;
skywing918 marked this conversation as resolved.
Show resolved Hide resolved
}

// Attach schema to emitObject if not empty
if (Object.keys(schema).length !== 0) {
emitObject.xml = schema;
}
}

reference(
targetDeclaration: Declaration<Record<string, unknown>>,
pathUp: Scope<Record<string, unknown>>[],
Expand Down Expand Up @@ -786,6 +838,8 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
"deprecated"
);

this.#attachXmlObject(program, type as ModelProperty, schema);
skywing918 marked this conversation as resolved.
Show resolved Hide resolved

this.#attachExtensions(program, type, schema);

const values = getKnownValues(program, type as any);
Expand Down
10 changes: 10 additions & 0 deletions packages/openapi3/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ export type JsonType = "array" | "boolean" | "integer" | "number" | "object" | "
*/
export type OpenAPI3SchemaProperty = Ref<OpenAPI3Schema> | OpenAPI3Schema;

export type OpenAPI3XmlSchema = Extensions & {
name?: string;
namespace?: string;
prefix?: string;
attribute?: boolean;
wrapped?: boolean;
};
skywing918 marked this conversation as resolved.
Show resolved Hide resolved
export type OpenAPI3Schema = Extensions & {
/**
* This attribute is a string that provides a short description of the instance property.
Expand Down Expand Up @@ -587,6 +594,9 @@ export type OpenAPI3Schema = Extensions & {

/** Specifies that a schema is deprecated and SHOULD be transitioned out of usage.Default value is false. */
deprecated?: boolean;

/** This MAY be used only on properties schemas. It has no effect on root schemas. Adds additional metadata to describe the XML representation of this property. */
xml?: any;
skywing918 marked this conversation as resolved.
Show resolved Hide resolved
};

export type OpenAPI3ParameterBase = Extensions & {
Expand Down
8 changes: 6 additions & 2 deletions packages/openapi3/test/test-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { HttpTestLibrary } from "@typespec/http/testing";
import { OpenAPITestLibrary } from "@typespec/openapi/testing";
import { RestTestLibrary } from "@typespec/rest/testing";
import { VersioningTestLibrary } from "@typespec/versioning/testing";
import { XmlTestLibrary } from "@typespec/xml/testing";
import { ok } from "assert";
import { OpenAPI3EmitterOptions } from "../src/lib.js";
import { OpenAPI3TestLibrary } from "../src/testing/index.js";
Expand All @@ -20,6 +21,7 @@ export async function createOpenAPITestHost() {
HttpTestLibrary,
RestTestLibrary,
VersioningTestLibrary,
XmlTestLibrary,
OpenAPITestLibrary,
OpenAPI3TestLibrary,
],
Expand All @@ -35,10 +37,12 @@ export async function createOpenAPITestRunner({
import "@typespec/rest";
import "@typespec/openapi";
import "@typespec/openapi3";
import "@typespec/xml";
${withVersioning ? `import "@typespec/versioning"` : ""};
using TypeSpec.Rest;
using TypeSpec.Http;
using TypeSpec.OpenAPI;
using TypeSpec.Xml;
${withVersioning ? "using TypeSpec.Versioning;" : ""}
`;
return createTestWrapper(host, {
Expand Down Expand Up @@ -86,9 +90,9 @@ export async function openApiFor(
const outPath = resolveVirtualPath("{version}.openapi.json");
host.addTypeSpecFile(
"./main.tsp",
`import "@typespec/http"; import "@typespec/rest"; import "@typespec/openapi"; import "@typespec/openapi3"; ${
`import "@typespec/http"; import "@typespec/rest"; import "@typespec/openapi"; import "@typespec/openapi3";import "@typespec/xml"; ${
versions ? `import "@typespec/versioning"; using TypeSpec.Versioning;` : ""
}using TypeSpec.Rest;using TypeSpec.Http;using TypeSpec.OpenAPI;${code}`
}using TypeSpec.Rest;using TypeSpec.Http;using TypeSpec.OpenAPI;using TypeSpec.Xml;${code}`
);
const diagnostics = await host.diagnose("./main.tsp", {
noEmit: false,
Expand Down
Loading
Loading