这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/seven-rings-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix(parser): prune `required` array after removing properties
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ export const failure_FailureSchema = {
export const ModelWithPropertiesWritableSchema = {
description: 'This is a model with one nested property',
type: 'object',
required: ['required', 'requiredAndReadOnly'],
required: ['required'],
properties: {
required: {
type: 'string'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ export const ModelWithNestedCompositionEnumsSchema = {

export const ModelWithReadOnlyAndWriteOnlySchema = {
type: 'object',
required: ['foo', 'bar', 'baz'],
required: ['foo', 'bar'],
properties: {
foo: {
type: 'string'
Expand Down Expand Up @@ -2002,7 +2002,7 @@ export const Generic_Schema_Duplicate_Issue_1_System_String_Schema = {
export const ModelWithPropertiesWritableSchema = {
description: 'This is a model with one nested property',
type: 'object',
required: ['required', 'requiredAndReadOnly', 'requiredAndNullable'],
required: ['required', 'requiredAndNullable'],
properties: {
required: {
type: 'string'
Expand Down Expand Up @@ -2087,7 +2087,7 @@ export const FileWritableSchema = {

export const ModelWithReadOnlyAndWriteOnlyWritableSchema = {
type: 'object',
required: ['foo', 'bar', 'baz'],
required: ['foo', 'baz'],
properties: {
foo: {
type: 'string'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,7 @@ export const ModelWithNestedCompositionEnumsSchema = {

export const ModelWithReadOnlyAndWriteOnlySchema = {
type: 'object',
required: ['foo', 'bar', 'baz'],
required: ['foo', 'bar'],
properties: {
foo: {
type: 'string'
Expand Down Expand Up @@ -1996,7 +1996,7 @@ export const Generic_Schema_Duplicate_Issue_1_System_String_Schema = {
export const ModelWithPropertiesWritableSchema = {
description: 'This is a model with one nested property',
type: 'object',
required: ['required', 'requiredAndReadOnly', 'requiredAndNullable'],
required: ['required', 'requiredAndNullable'],
properties: {
required: {
type: 'string'
Expand Down Expand Up @@ -2080,7 +2080,7 @@ export const FileWritableSchema = {

export const ModelWithReadOnlyAndWriteOnlyWritableSchema = {
type: 'object',
required: ['foo', 'bar', 'baz'],
required: ['foo', 'baz'],
properties: {
foo: {
type: 'string'
Expand Down
42 changes: 35 additions & 7 deletions packages/openapi-ts/src/openApi/shared/transforms/readWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,9 @@ const pruneSchemaByScope = (
scope: 'readOnly' | 'writeOnly',
): boolean => {
if (schema && typeof schema === 'object') {
// Remove $ref if the referenced schema is exclusively the excluded scope
if (
'$ref' in schema &&
typeof (schema as Record<string, unknown>)['$ref'] === 'string'
) {
const ref = (schema as Record<string, unknown>)['$ref'] as string;
const nodeInfo = graph.nodes.get(ref);
// Handle $ref schemas
if ('$ref' in schema && typeof schema.$ref === 'string') {
const nodeInfo = graph.nodes.get(schema.$ref);
if (nodeInfo?.scopes) {
// Only remove $ref if the referenced schema is *exclusively* the excluded scope.
// This ensures 'normal' or multi-scope schemas are always kept.
Expand Down Expand Up @@ -197,6 +193,9 @@ const pruneSchemaByScope = (
!(value instanceof Array)
) {
const objMap = value as Record<string, unknown>;
// Track removed properties for object schemas to update required array
const removedProperties = new Set<string>();

for (const key of Object.keys(objMap)) {
const prop = objMap[key];
if (
Expand All @@ -205,13 +204,42 @@ const pruneSchemaByScope = (
(prop as Record<string, unknown>)[scope] === true
) {
delete objMap[key];
// Track removed properties for object schemas
if (keyword === 'properties') {
removedProperties.add(key);
}
} else {
const shouldRemove = pruneSchemaByScope(graph, prop, scope);
if (shouldRemove) {
delete objMap[key];
// Track removed properties for object schemas
if (keyword === 'properties') {
removedProperties.add(key);
}
}
}
}

// Update required array if properties were removed
if (
removedProperties.size > 0 &&
keyword === 'properties' &&
'required' in schema &&
Array.isArray((schema as Record<string, unknown>).required)
) {
const required = (schema as Record<string, unknown>)
.required as string[];
const filteredRequired = required.filter(
(prop) => !removedProperties.has(prop),
);

if (filteredRequired.length === 0) {
delete (schema as Record<string, unknown>).required;
} else {
(schema as Record<string, unknown>).required = filteredRequired;
}
}

if (!Object.keys(objMap).length) {
delete (schema as Record<string, unknown>)[keyword];
}
Expand Down
Loading