Skip to content

Commit

Permalink
Changed JSON extension example to prevent incompatibilities with GemS…
Browse files Browse the repository at this point in the history
…tone protocol
  • Loading branch information
mtabacman committed May 24, 2024
1 parent 3c26f44 commit 6bf5eec
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ PetOrdersRESTfulControllerTest >> createOverlyComplexOrder [

^ resourceController
createOrderBasedOn: ( self requestToPOSTAsOverlyComplexOrder:
( '{"date":{"date":{"year":2018,"month":10,"day":24,"offset":0},"time":"18:05:46.418Z"},"pet":"<1p>"}'
expandMacrosWith: self petUrl ) )
'{"date":"2018-10-24T18:05:46.418Z","pet":{"alternativeName":"Fido","itsType":"Dog","theStatus":"happy"}}' )
within: self newHttpRequestContext
]

Expand Down Expand Up @@ -531,10 +530,10 @@ PetOrdersRESTfulControllerTest >> testOverlyComplexOrderCreation [
assert: orderRepository count equals: 1.
order := orderRepository findAll first.
self
assert: order pet equals: self petUrl;
assert: order date equals: ( DateAndTime
date: ( Date readFrom: '2018-10-24' pattern: 'yyyy-mm-dd' )
time: ( Time fromString: '18:05:46.418' ) )
assert: order pet name equals: 'Fido';
assert: order pet type equals: 'Dog';
assert: order pet status equals: 'HAPPY';
assert: order date equals: '2018-10-24T18:05:46.418Z'
]

{ #category : 'tests' }
Expand Down
26 changes: 10 additions & 16 deletions source/Stargate-Examples/PetOrdersRESTfulController.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ PetOrdersRESTfulController >> configureOrderDecodingOn: reader [
PetOrdersRESTfulController >> configureOrderEncodingOn: writer within: requestContext [

writer
for: DateAndTime
customDo: [ :mapping | mapping encoder: [ :dateAndTime | dateAndTime printString ] ];
for: Pet do: [ :mapping | mapping mapInstVars ];
for: ZnUrl customDo: [ :mapping | mapping encoder: [ :url | url printString ] ];
for: #Order do: [ :mapping |
mapping
Expand All @@ -143,25 +142,20 @@ PetOrdersRESTfulController >> configureOrderEncodingOn: writer within: requestCo
{ #category : 'private' }
PetOrdersRESTfulController >> configureOverlyComplexOrderDecodingOn: reader [

reader for: #Url customDo: [ :mapping | mapping decoder: [ :string | string asUrl ] ].
reader for: #Time customDo: [ :mapping | mapping decoder: [ :string | string asTime ] ].
reader for: Date createInstanceUsing: [ :mapping |
reader for: #Status customDo: [ :mapping | mapping decoder: [ :string | string asUppercase ] ].
reader for: Pet createInstanceUsing: [ :mapping |
mapping
mapProperty: #year;
mapProperty: #month;
mapProperty: #day.
mapping mapCreationSending: #newDay:month:year: withArguments: { #day. #month. #year }
].
reader for: DateAndTime createInstanceUsing: [ :mapping |
mapProperty: #alternativeName;
mapProperty: #itsType;
mapProperty: #theStatus as: #Status.
mapping
mapProperty: #date as: Date;
mapProperty: #time as: #Time.
mapping mapCreationSending: #date:time: withArguments: { #date. #time }
mapCreationSending: #named:ofType:withStatus:
withArguments: { #alternativeName. #itsType. #theStatus }
].
reader for: PetOrder createInstanceUsing: [ :mapping |
mapping
mapProperty: #date as: DateAndTime;
mapProperty: #pet as: #Url.
mapProperty: #date;
mapProperty: #pet as: Pet.
mapping mapCreationSending: #for:on: withArguments: { #pet. #date }
].

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ I will fail on reading properties of an object if some of the mapped properties
"
Class {
#name : 'ValidCompleteInstanceMapping',
#name : 'InstanceCreationMapping',
#superclass : 'NeoJSONObjectMapping',
#instVars : [
'instanceCreationSelector',
Expand All @@ -15,7 +15,7 @@ Class {
}

{ #category : 'private' }
ValidCompleteInstanceMapping >> errorDescriptionForMissing: propertyNames [
InstanceCreationMapping >> errorDescriptionForMissing: propertyNames [

^ String streamContents: [ :stream |
stream
Expand All @@ -38,14 +38,14 @@ ValidCompleteInstanceMapping >> errorDescriptionForMissing: propertyNames [
]

{ #category : 'mapping' }
ValidCompleteInstanceMapping >> mapCreationSending: anInstanceCreationSelector withArguments: anArgumentCollection [
InstanceCreationMapping >> mapCreationSending: anInstanceCreationSelector withArguments: anArgumentCollection [

instanceCreationSelector := anInstanceCreationSelector.
argumentNames := anArgumentCollection
]

{ #category : 'mapping' }
ValidCompleteInstanceMapping >> mapProperty: aKey [
InstanceCreationMapping >> mapProperty: aKey [

^ self
mapProperty: aKey
Expand All @@ -54,13 +54,13 @@ ValidCompleteInstanceMapping >> mapProperty: aKey [
]

{ #category : 'mapping' }
ValidCompleteInstanceMapping >> mapProperty: aKey as: aValueSchema [
InstanceCreationMapping >> mapProperty: aKey as: aValueSchema [

( self mapProperty: aKey ) valueSchema: aValueSchema
]

{ #category : 'parsing' }
ValidCompleteInstanceMapping >> readFrom: jsonReader [
InstanceCreationMapping >> readFrom: jsonReader [

| argumentByName arguments missingArguments |

Expand Down
10 changes: 5 additions & 5 deletions source/Stargate-NeoJSON-Extensions/NeoJSONReader.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ NeoJSONReader >> for: schemaName createInstanceUsing: block [

| mapping |

mapping := self validCompleteInstanceMappingFor: schemaName.
mapping := self instanceCreationMappingFor: schemaName.
block value: mapping.
^ mapping
]
Expand All @@ -21,20 +21,20 @@ NeoJSONReader >> for: schemaName strictDo: block [
]

{ #category : '*Stargate-NeoJSON-Extensions' }
NeoJSONReader >> strictMappingFor: smalltalkClass [
NeoJSONReader >> instanceCreationMappingFor: smalltalkClass [

^ self mappings at: smalltalkClass ifAbsentPut: [
NeoJSONStrictObjectMapping new
InstanceCreationMapping new
subjectClass: smalltalkClass;
yourself
]
]

{ #category : '*Stargate-NeoJSON-Extensions' }
NeoJSONReader >> validCompleteInstanceMappingFor: smalltalkClass [
NeoJSONReader >> strictMappingFor: smalltalkClass [

^ self mappings at: smalltalkClass ifAbsentPut: [
ValidCompleteInstanceMapping new
NeoJSONStrictObjectMapping new
subjectClass: smalltalkClass;
yourself
]
Expand Down

0 comments on commit 6bf5eec

Please sign in to comment.