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 all 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
7 changes: 7 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,7 @@
---
changeKind: feature
packages:
- "@typespec/openapi3"
---

Add XML support using `@typespec/xml` library
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ coverage.xml
.pytest_cache/
test-results.xml
test-results/
__snapshots__/

# Translations
*.mo
Expand Down Expand Up @@ -224,4 +225,3 @@ BenchmarkDotnet.Artifacts/
packages/http-client-python/generator/test/**/generated/
packages/http-client-python/generator/test/**/cadl-ranch-coverage.json
!packages/http-client-python/package-lock.json

6 changes: 6 additions & 0 deletions packages/openapi3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
"@typespec/openapi": "workspace:~",
"@typespec/versioning": "workspace:~"
},
"peerDependenciesMeta": {
"@typespec/xml": {
"optional": true
}
},
"devDependencies": {
"@types/node": "~22.7.5",
"@types/yargs": "~17.0.33",
Expand All @@ -77,6 +82,7 @@
"@typespec/rest": "workspace:~",
"@typespec/tspd": "workspace:~",
"@typespec/versioning": "workspace:~",
"@typespec/xml": "workspace:~",
"@vitest/coverage-v8": "^2.1.2",
"@vitest/ui": "^2.1.2",
"c8": "^10.1.2",
Expand Down
12 changes: 12 additions & 0 deletions packages/openapi3/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@ export const libDef = {
default: paramMessage`Authentication "${"authType"}" is not a known authentication by the openapi3 emitter, it will be ignored.`,
},
},
"xml-attribute-invalid-property-type": {
severity: "warning",
messages: {
default: paramMessage`XML \`@attribute\` can only be primitive types in the OpenAPI 3 emitter, Property '${"name"}' type will be changed to type: string.`,
},
},
"xml-unwrapped-invalid-property-type": {
severity: "warning",
messages: {
default: paramMessage`XML \`@unwrapped\` can only used on array properties or primitive ones in the OpenAPI 3 emitter, Property '${"name"}' will be ignored.`,
},
},
},
emitter: {
options: EmitterOptionsSchema as JSONSchemaType<OpenAPI3EmitterOptions>,
Expand Down
8 changes: 6 additions & 2 deletions packages/openapi3/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ import {
} from "./types.js";
import { deepEquals, isSharedHttpOperation, SharedHttpOperation } from "./util.js";
import { resolveVisibilityUsage, VisibilityUsageTracker } from "./visibility-usage.js";
import { resolveXmlModule, XmlModule } from "./xml-module.js";

const defaultFileType: FileType = "yaml";
const defaultOptions = {
Expand Down Expand Up @@ -284,6 +285,7 @@ function createOAPIEmitter(
service: Service,
allHttpAuthentications: HttpAuth[],
defaultAuth: AuthenticationReference,
xmlModule: XmlModule | undefined,
version?: string,
) {
diagnostics = createDiagnosticCollector();
Expand All @@ -298,11 +300,12 @@ function createOAPIEmitter(
service.type,
options.omitUnreachableTypes,
);

schemaEmitter = createAssetEmitter(
program,
class extends OpenAPI3SchemaEmitter {
constructor(emitter: AssetEmitter<Record<string, any>, OpenAPI3EmitterOptions>) {
super(emitter, metadataInfo, visibilityUsage, options);
super(emitter, metadataInfo, visibilityUsage, options, xmlModule);
}
} as any,
context,
Expand Down Expand Up @@ -615,7 +618,8 @@ function createOAPIEmitter(
const httpService = ignoreDiagnostics(getHttpService(program, service.type));
const auth = (serviceAuth = resolveAuthentication(httpService));

initializeEmitter(service, auth.schemes, auth.defaultAuth, version);
const xmlModule = await resolveXmlModule();
initializeEmitter(service, auth.schemes, auth.defaultAuth, xmlModule, version);
reportIfNoRoutes(program, httpService.operations);

for (const op of resolveOperations(httpService.operations)) {
Expand Down
37 changes: 32 additions & 5 deletions packages/openapi3/src/schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import {
OpenAPI3SchemaProperty,
} from "./types.js";
import { VisibilityUsageTracker } from "./visibility-usage.js";
import { XmlModule } from "./xml-module.js";

/**
* OpenAPI3 schema emitter. Deals with emitting content of `components/schemas` section.
Expand All @@ -95,16 +96,19 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
#metadataInfo: MetadataInfo;
#visibilityUsage: VisibilityUsageTracker;
#options: ResolvedOpenAPI3EmitterOptions;
#xmlModule: XmlModule | undefined;
constructor(
emitter: AssetEmitter<Record<string, any>, OpenAPI3EmitterOptions>,
metadataInfo: MetadataInfo,
visibilityUsage: VisibilityUsageTracker,
options: ResolvedOpenAPI3EmitterOptions,
xmlModule: XmlModule | undefined,
) {
super(emitter);
this.#metadataInfo = metadataInfo;
this.#visibilityUsage = visibilityUsage;
this.#options = options;
this.#xmlModule = xmlModule;
}

modelDeclarationReferenceContext(model: Model, name: string): Context {
Expand Down Expand Up @@ -382,7 +386,7 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
const schema = this.#applyEncoding(prop, refSchema.value as any);

// Apply decorators on the property to the type's schema
const additionalProps: Partial<OpenAPI3Schema> = this.#applyConstraints(prop, {});
const additionalProps: Partial<OpenAPI3Schema> = this.#applyConstraints(prop, {}, schema);
if (prop.defaultValue) {
additionalProps.default = getDefaultValue(program, prop.defaultValue, prop);
}
Expand All @@ -398,10 +402,14 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
if (Object.keys(additionalProps).length === 0) {
return schema;
} else {
return {
allOf: [schema],
...additionalProps,
};
if (additionalProps.xml?.attribute) {
return additionalProps;
} else {
return {
allOf: [schema],
...additionalProps,
};
}
}
} else {
if (getOneOf(program, prop) && schema.anyOf) {
Expand Down Expand Up @@ -765,6 +773,7 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
#applyConstraints(
type: Scalar | Model | ModelProperty | Union | Enum,
original: OpenAPI3Schema,
refSchema?: OpenAPI3Schema,
): ObjectBuilder<OpenAPI3Schema> {
const schema = new ObjectBuilder(original);
const program = this.emitter.getProgram();
Expand Down Expand Up @@ -813,6 +822,24 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
"deprecated",
);

if (this.#xmlModule) {
skywing918 marked this conversation as resolved.
Show resolved Hide resolved
switch (type.kind) {
case "Scalar":
case "Model":
this.#xmlModule.attachXmlObjectForScalarOrModel(program, type, schema);
break;
case "ModelProperty":
this.#xmlModule.attachXmlObjectForModelProperty(
program,
this.#options,
type,
schema,
refSchema,
);
break;
}
}

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

const values = getKnownValues(program, type as any);
Expand Down
11 changes: 11 additions & 0 deletions packages/openapi3/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,14 @@ 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 +595,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?: OpenAPI3XmlSchema;
};

export type OpenAPI3ParameterBase = Extensions & {
Expand Down
Loading
Loading