From 68bab55cd0e88504201e6d06f5367bef3738e561 Mon Sep 17 00:00:00 2001 From: Mariano Saura Date: Fri, 27 Sep 2024 11:01:50 -0300 Subject: [PATCH 1/2] Defaults for non-mandatory arguments in instance creation json mapping --- .../InstanceCreationMapping.class.st | 53 +++++++++++++------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/source/Stargate-NeoJSON-Extensions/InstanceCreationMapping.class.st b/source/Stargate-NeoJSON-Extensions/InstanceCreationMapping.class.st index 340cd7d..464af08 100644 --- a/source/Stargate-NeoJSON-Extensions/InstanceCreationMapping.class.st +++ b/source/Stargate-NeoJSON-Extensions/InstanceCreationMapping.class.st @@ -8,12 +8,25 @@ Class { #superclass : 'NeoJSONObjectMapping', #instVars : [ 'instanceCreationSelector', - 'argumentNames' + 'argumentNames', + 'defaultsByArgumentName' ], #category : 'Stargate-NeoJSON-Extensions', #package : 'Stargate-NeoJSON-Extensions' } +{ #category : 'mapping' } +InstanceCreationMapping >> argumentNamed: anArgumentName defaultingTo: aValue [ + + defaultsByArgumentName at: anArgumentName put: aValue +] + +{ #category : 'private' } +InstanceCreationMapping >> defaultFor: argumentName ifAbsent: aBlock [ + + ^ defaultsByArgumentName at: argumentName ifAbsent: aBlock +] + { #category : 'private' } InstanceCreationMapping >> errorDescriptionForMissing: propertyNames [ @@ -40,8 +53,9 @@ InstanceCreationMapping >> errorDescriptionForMissing: propertyNames [ { #category : 'mapping' } InstanceCreationMapping >> mapCreationSending: anInstanceCreationSelector withArguments: anArgumentCollection [ - instanceCreationSelector := anInstanceCreationSelector. - argumentNames := anArgumentCollection + instanceCreationSelector := anInstanceCreationSelector. + argumentNames := anArgumentCollection. + defaultsByArgumentName := Dictionary new ] { #category : 'mapping' } @@ -62,20 +76,25 @@ InstanceCreationMapping >> mapProperty: aKey as: aValueSchema [ { #category : 'parsing' } InstanceCreationMapping >> readFrom: jsonReader [ - | argumentByName arguments missingArguments | - - argumentByName := Dictionary new. - jsonReader parseMapKeysDo: [ :key | - ( self propertyNamed: key ifAbsent: [ nil ] ) - ifNil: [ "read, skip & ignore value" jsonReader next ] - ifNotNil: [ :mapping | mapping readObject: argumentByName from: jsonReader ] - ]. + | argumentByName arguments missingArguments | + argumentByName := Dictionary new. + jsonReader parseMapKeysDo: [ :key | + ( self propertyNamed: key ifAbsent: [ nil ] ) + ifNil: [ "read, skip & ignore value" jsonReader next ] + ifNotNil: [ :mapping | mapping readObject: argumentByName from: jsonReader ] + ]. - missingArguments := OrderedCollection new. - arguments := argumentNames collect: [ :argumentName | - argumentByName at: argumentName ifAbsent: [ missingArguments add: argumentName ] ]. - missingArguments ifNotEmpty: [ - jsonReader error: ( self errorDescriptionForMissing: missingArguments ) ]. + missingArguments := OrderedCollection new. + arguments := argumentNames collect: [ :argumentName | + argumentByName + at: argumentName + ifAbsent: [ + self + defaultFor: argumentName + ifAbsent: [ missingArguments add: argumentName ] ] + ]. + missingArguments ifNotEmpty: [ + jsonReader error: ( self errorDescriptionForMissing: missingArguments ) ]. - ^ subjectClass perform: instanceCreationSelector withArguments: arguments + ^ subjectClass perform: instanceCreationSelector withArguments: arguments ] From c7a09c8b1ffa79f764ebcc6d52b917261eaffe21 Mon Sep 17 00:00:00 2001 From: Mariano Saura Date: Fri, 27 Sep 2024 16:10:37 -0300 Subject: [PATCH 2/2] Test case added --- .../PetOrdersRESTfulControllerTest.class.st | 24 +++++++++++++++++++ .../PetOrdersRESTfulController.class.st | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/source/Stargate-Examples-Tests/PetOrdersRESTfulControllerTest.class.st b/source/Stargate-Examples-Tests/PetOrdersRESTfulControllerTest.class.st index fc60e29..591ce6f 100644 --- a/source/Stargate-Examples-Tests/PetOrdersRESTfulControllerTest.class.st +++ b/source/Stargate-Examples-Tests/PetOrdersRESTfulControllerTest.class.st @@ -536,6 +536,30 @@ PetOrdersRESTfulControllerTest >> testOverlyComplexOrderCreation [ assert: order date equals: '2018-10-24T18:05:46.418Z' ] +{ #category : 'tests - orders' } +PetOrdersRESTfulControllerTest >> testOverlyComplexOrderCreationWithoutSpecifyingStatusUsesDefaultValue [ + + | response order | + + response := resourceController + createOrderBasedOn: ( self requestToPOSTAsOverlyComplexOrder: + '{"date":"2018-10-24T18:05:46.418Z","pet":{"alternativeName":"Fido","itsType":"Dog"}}' ) + within: self newHttpRequestContext. + + self + assert: response isSuccess; + assert: response status equals: 201; + assertUrl: response location equals: 'https://petstore.example.com/orders/1'; + assert: response hasEntity; + assert: orderRepository count equals: 1. + order := orderRepository findAll first. + self + assert: order pet name equals: 'Fido'; + assert: order pet type equals: 'Dog'; + assert: order pet status equals: 'UNKNOWN'; + assert: order date equals: '2018-10-24T18:05:46.418Z' +] + { #category : 'tests' } PetOrdersRESTfulControllerTest >> testRoutes [ diff --git a/source/Stargate-Examples/PetOrdersRESTfulController.class.st b/source/Stargate-Examples/PetOrdersRESTfulController.class.st index 30f8e25..c1df0c7 100644 --- a/source/Stargate-Examples/PetOrdersRESTfulController.class.st +++ b/source/Stargate-Examples/PetOrdersRESTfulController.class.st @@ -150,7 +150,8 @@ PetOrdersRESTfulController >> configureOverlyComplexOrderDecodingOn: reader [ mapProperty: #theStatus as: #Status. mapping mapCreationSending: #named:ofType:withStatus: - withArguments: { #alternativeName. #itsType. #theStatus } + withArguments: { #alternativeName. #itsType. #theStatus }; + argumentNamed: #theStatus defaultingTo: 'UNKNOWN' ]. reader for: PetOrder createInstanceUsing: [ :mapping | mapping