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
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

const componentSchemas = {
schemas: {
SchemaWithReadOnly: {
type: 'object',
properties: {
id: {
type: 'string',
readOnly: true,
},
name: {
type: 'string',
},
},
},
SchemaWithoutReadOnly: {
type: 'object',
properties: {
name: {
type: 'string',
},
description: {
type: 'string',
},
},
},
NestedSchemaWithReadOnly: {
type: 'object',
properties: {
user: {
type: 'object',
properties: {
userId: {
type: 'string',
readOnly: true,
},
username: {
type: 'string',
},
},
},
},
},
ArraySchemaWithReadOnly: {
type: 'object',
properties: {
items: {
type: 'array',
items: {
type: 'object',
properties: {
itemId: {
type: 'string',
readOnly: true,
},
itemName: {
type: 'string',
},
},
},
},
},
},
},
};

testRule('xgen-IPA-106-create-method-request-has-no-readonly-fields', [
{
name: 'valid methods - no readOnly fields',
document: {
components: componentSchemas,
paths: {
'/valid-resource': {
post: {
requestBody: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
$ref: '#/components/schemas/SchemaWithoutReadOnly',
},
},
'application/vnd.atlas.2024-01-01+json': {
schema: {
type: 'string',
},
},
},
},
},
},
},
},
errors: [],
},
{
name: 'valid methods - custom method can have readOnly fields',
document: {
components: componentSchemas,
paths: {
'/resource:customAction': {
post: {
requestBody: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
$ref: '#/components/schemas/SchemaWithReadOnly',
},
},
},
},
},
},
},
},
errors: [],
},
{
name: 'invalid methods - direct readOnly field',
document: {
components: componentSchemas,
paths: {
'/invalid-resource': {
post: {
requestBody: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
$ref: '#/components/schemas/SchemaWithReadOnly',
},
},
'application/vnd.atlas.2024-01-01+json': {
schema: {
type: 'string',
readOnly: true,
},
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-106-create-method-request-has-no-readonly-fields',
message:
'The Create method request object must not include input fields (readOnly properties). Found readOnly property at: id. http://go/ipa/106',
path: ['paths', '/invalid-resource', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-106-create-method-request-has-no-readonly-fields',
message:
'The Create method request object must not include input fields (readOnly properties). Found readOnly property at one of the inline schemas. http://go/ipa/106',
path: ['paths', '/invalid-resource', 'post', 'requestBody', 'content', 'application/vnd.atlas.2024-01-01+json'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid methods - nested readOnly field',
document: {
components: componentSchemas,
paths: {
'/nested-invalid-resource': {
post: {
requestBody: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
$ref: '#/components/schemas/NestedSchemaWithReadOnly',
},
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-106-create-method-request-has-no-readonly-fields',
message:
'The Create method request object must not include input fields (readOnly properties). Found readOnly property at: user.userId. http://go/ipa/106',
path: [
'paths',
'/nested-invalid-resource',
'post',
'requestBody',
'content',
'application/vnd.atlas.2023-01-01+json',
],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid methods - array with readOnly field',
document: {
components: componentSchemas,
paths: {
'/array-invalid-resource': {
post: {
requestBody: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
$ref: '#/components/schemas/ArraySchemaWithReadOnly',
},
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-106-create-method-request-has-no-readonly-fields',
message:
'The Create method request object must not include input fields (readOnly properties). Found readOnly property at: items.items.itemId. http://go/ipa/106',
path: [
'paths',
'/array-invalid-resource',
'post',
'requestBody',
'content',
'application/vnd.atlas.2023-01-01+json',
],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'methods with exceptions',
document: {
components: componentSchemas,
paths: {
'/excepted-resource': {
post: {
requestBody: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
$ref: '#/components/schemas/SchemaWithReadOnly',
},
'x-xgen-IPA-exception': {
'xgen-IPA-106-create-method-request-has-no-readonly-fields': 'Reason',
},
},
},
},
},
},
},
},
errors: [],
},
]);
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ testRule('xgen-IPA-104-get-method-response-has-no-input-fields', [
{
code: 'xgen-IPA-104-get-method-response-has-no-input-fields',
message:
'The get method response object must not include output fields (writeOnly properties). http://go/ipa/104',
'The get method response object must not include output fields (writeOnly properties). Found writeOnly property at: name. http://go/ipa/104',
path: [
'paths',
'/resource/{id}',
Expand All @@ -146,7 +146,7 @@ testRule('xgen-IPA-104-get-method-response-has-no-input-fields', [
{
code: 'xgen-IPA-104-get-method-response-has-no-input-fields',
message:
'The get method response object must not include output fields (writeOnly properties). http://go/ipa/104',
'The get method response object must not include output fields (writeOnly properties). Found writeOnly property at: name. http://go/ipa/104',
path: [
'paths',
'/resource/{id}/singleton',
Expand Down
Loading
Loading