Skip to content

Commit

Permalink
fix(cli): fix in-line Alias, base properties, and extended properties…
Browse files Browse the repository at this point in the history
… example generation. (#5922)
  • Loading branch information
eyw520 authored Feb 7, 2025
1 parent 99af940 commit d73e105
Show file tree
Hide file tree
Showing 1,259 changed files with 53,435 additions and 3,788 deletions.
7 changes: 7 additions & 0 deletions packages/cli/cli/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
- changelogEntry:
- summary: |
Type reference example generation now handles extends and base properties correctly, as well as in-lined Alias types.
type: fix
irVersion: 55
version: 0.51.36

- changelogEntry:
- summary: |
The cli will now respect examples with `null` values in OpenAPI specs. This will allow for null properties to show up when using
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3303,7 +3303,7 @@
"autogeneratedExamples": [
{
"example": {
"id": "ca26b95a",
"id": "f05c72fe",
"url": "/movies/id",
"name": null,
"endpointHeaders": [],
Expand Down Expand Up @@ -4022,7 +4022,8 @@
}
},
"jsonExample": {
"type": "actor"
"type": "actor",
"name": "name"
}
},
{
Expand Down Expand Up @@ -4147,7 +4148,8 @@
}
},
"jsonExample": {
"type": "actor"
"type": "actor",
"name": "name"
}
}
],
Expand Down Expand Up @@ -4223,10 +4225,12 @@
},
"jsonExample": [
{
"type": "actor"
"type": "actor",
"name": "name"
},
{
"type": "actor"
"type": "actor",
"name": "name"
}
]
}
Expand Down Expand Up @@ -4305,10 +4309,12 @@
"rating": 1.1,
"cast": [
{
"type": "actor"
"type": "actor",
"name": "name"
},
{
"type": "actor"
"type": "actor",
"name": "name"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {

import { hashJSON } from "../../utils/hashJSON";
import { ExampleGenerationResult } from "./ExampleGenerationResult";
import { generateTypeDeclarationExample } from "./generateTypeDeclarationExample";
import { generateTypeReferenceExample } from "./generateTypeReferenceExample";
import { isOptional } from "./isTypeReferenceOptional";

Expand Down Expand Up @@ -131,7 +132,7 @@ export function generateEndpointExample({
const generatedExample = generateTypeReferenceExample({
fieldName: queryParameter.name.name.originalName,
currentDepth: 0,
maxDepth: 1,
maxDepth: 10,
typeDeclarations,
typeReference: queryParameter.valueType,
skipOptionalProperties: skipOptionalRequestProperties
Expand Down Expand Up @@ -193,6 +194,28 @@ export function generateEndpointExample({
case "inlinedRequestBody": {
const jsonExample: Record<string, unknown> = {};
const properties: ExampleInlinedRequestBodyProperty[] = [];

if (endpoint.requestBody.extends != null) {
for (const extendedTypeReference of endpoint.requestBody.extends) {
const extendedTypeDeclaration = typeDeclarations[extendedTypeReference.typeId];
if (extendedTypeDeclaration == null) {
throw new Error(
`Failed to find extended type declaration with id ${extendedTypeReference.typeId}`
);
}
const extendedExample = generateTypeDeclarationExample({
typeDeclaration: extendedTypeDeclaration,
typeDeclarations,
currentDepth: 1,
maxDepth: 10,
skipOptionalProperties: skipOptionalRequestProperties
});
if (extendedExample.type === "success") {
Object.assign(jsonExample, extendedExample.jsonExample);
}
}
}

for (const property of [
...(endpoint.requestBody.properties ?? []),
...(endpoint.requestBody.extendedProperties ?? [])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
ExampleAliasType,
ExampleObjectProperty,
ExampleObjectType,
ExampleSingleUnionTypeProperties,
Expand Down Expand Up @@ -71,6 +70,32 @@ export function generateTypeDeclarationExample({
};
}
case "object": {
const baseJsonExample: Record<string, unknown> = {};
const baseProperties: ExampleObjectProperty[] = [];

if (typeDeclaration.shape.extends != null) {
for (const extendedTypeReference of typeDeclaration.shape.extends) {
const extendedTypeDeclaration = typeDeclarations[extendedTypeReference.typeId];
if (extendedTypeDeclaration == null) {
throw new Error(
`Failed to find extended type declaration with id ${extendedTypeReference.typeId}`
);
}
const extendedExample = generateTypeDeclarationExample({
fieldName,
typeDeclaration: extendedTypeDeclaration,
typeDeclarations,
currentDepth: currentDepth + 1,
maxDepth,
skipOptionalProperties
});
if (extendedExample.type === "success" && extendedExample.example.type === "object") {
Object.assign(baseJsonExample, extendedExample.jsonExample);
baseProperties.push(...extendedExample.example.properties);
}
}
}

const objectExample = generateObjectDeclarationExample({
fieldName,
typeDeclaration,
Expand All @@ -86,8 +111,11 @@ export function generateTypeDeclarationExample({
const { example, jsonExample } = objectExample;
return {
type: "success",
example: ExampleTypeShape.object(example),
jsonExample
example: ExampleTypeShape.object({
...example,
properties: [...baseProperties, ...example.properties]
}),
jsonExample: Object.assign({}, baseJsonExample, jsonExample)
};
}
case "undiscriminatedUnion": {
Expand Down Expand Up @@ -119,6 +147,23 @@ export function generateTypeDeclarationExample({
}
case "union": {
const discriminant = typeDeclaration.shape.discriminant;
const basePropertyExamples: Record<string, unknown> = {};
if (typeDeclaration.shape.baseProperties != null) {
for (const baseProperty of typeDeclaration.shape.baseProperties) {
const basePropertyExample = generateTypeReferenceExample({
fieldName: baseProperty.name.wireValue,
typeReference: baseProperty.valueType,
typeDeclarations,
currentDepth: currentDepth + 1,
maxDepth,
skipOptionalProperties
});
if (basePropertyExample.type === "success") {
basePropertyExamples[baseProperty.name.wireValue] = basePropertyExample.jsonExample;
}
}
}

for (const variant of typeDeclaration.shape.types) {
const variantExample = variant.shape._visit<ExampleGenerationResult<ExampleTypeShape>>({
noProperties: () => {
Expand All @@ -132,7 +177,8 @@ export function generateTypeDeclarationExample({
}
}),
jsonExample: {
[discriminant.wireValue]: variant.discriminantValue.wireValue
[discriminant.wireValue]: variant.discriminantValue.wireValue,
...basePropertyExamples
}
};
},
Expand Down Expand Up @@ -173,7 +219,8 @@ export function generateTypeDeclarationExample({
}),
jsonExample: {
[discriminant.wireValue]: variant.discriminantValue.wireValue,
...(typeof jsonExample === "object" ? jsonExample : {})
...(typeof jsonExample === "object" ? jsonExample : {}),
...basePropertyExamples
}
};
},
Expand Down Expand Up @@ -201,7 +248,8 @@ export function generateTypeDeclarationExample({
}),
jsonExample: {
[discriminant.wireValue]: variant.discriminantValue.wireValue,
[value.name.wireValue]: jsonExample
[value.name.wireValue]: jsonExample,
...basePropertyExamples
}
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@
"queryParameters": {},
"headers": {},
"requestBody": {
"parent": "parent",
"child": "child"
},
"requestBodyV3": {
"type": "json",
"value": {
"parent": "parent",
"child": "child"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,15 @@
"queryParameters": {},
"headers": {},
"requestBody": {
"docs": "docs",
"name": "name",
"unique": "unique"
},
"requestBodyV3": {
"type": "json",
"value": {
"docs": "docs",
"name": "name",
"unique": "unique"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1999,11 +1999,37 @@
"headers": {},
"responseStatusCode": 200,
"responseBody": {
"data": {
"users": [
{
"name": "name",
"id": 1
},
{
"name": "name",
"id": 1
}
]
},
"next": "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
"total_count": 1
},
"responseBodyV3": {
"type": "json",
"value": {
"data": {
"users": [
{
"name": "name",
"id": 1
},
{
"name": "name",
"id": 1
}
]
},
"next": "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
"total_count": 1
}
},
Expand Down Expand Up @@ -2063,11 +2089,37 @@
"headers": {},
"responseStatusCode": 200,
"responseBody": {
"data": {
"users": [
{
"name": "name",
"id": 1
},
{
"name": "name",
"id": 1
}
]
},
"next": "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
"total_count": 1
},
"responseBodyV3": {
"type": "json",
"value": {
"data": {
"users": [
{
"name": "name",
"id": 1
},
{
"name": "name",
"id": 1
}
]
},
"next": "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
"total_count": 1
}
},
Expand Down
Loading

0 comments on commit d73e105

Please sign in to comment.