@@ -15,11 +15,12 @@ type ServiceDefinition struct {
1515 Resources map [string ]* Resource
1616}
1717
18- func GetServiceDefinition2 (api * openapi.OpenAPI ) (* ServiceDefinition , error ) {
18+ func GetServiceDefinition (api * openapi.OpenAPI , pathPrefix string ) (* ServiceDefinition , error ) {
1919 resourceBySingular := make (map [string ]* Resource )
2020 // we try to parse the paths to find possible resources, since
2121 // they may not always be annotated as such.
2222 for path , pathItem := range api .Paths {
23+ path = strings .TrimPrefix (path , pathPrefix )
2324 var r Resource
2425 var sRef * openapi.Schema
2526 p := getPatternInfo (path )
@@ -103,7 +104,7 @@ func GetServiceDefinition2(api *openapi.OpenAPI) (*ServiceDefinition, error) {
103104 // get the first serverURL url
104105 serverURL := ""
105106 for _ , s := range api .Servers {
106- serverURL = s .URL
107+ serverURL = s .URL + pathPrefix
107108 }
108109 if serverURL == "" {
109110 return nil , errors .New ("no servers found in the OpenAPI definition. Cannot find a server to send a request to" )
@@ -115,31 +116,6 @@ func GetServiceDefinition2(api *openapi.OpenAPI) (*ServiceDefinition, error) {
115116 }, nil
116117}
117118
118- func GetServiceDefinition (api * openapi.OpenAPI ) (* ServiceDefinition , error ) {
119- resources := make (map [string ]* Resource )
120- for name , s := range api .Components .Schemas {
121- if s .XAEPResource != nil {
122- _ , err := addResourceToMap (s , resources , api )
123- if err != nil {
124- return nil , fmt .Errorf ("error adding resource %q to map: %v" , name , err )
125- }
126- }
127- }
128- // get the first serverURL url
129- serverURL := ""
130- for _ , s := range api .Servers {
131- serverURL = s .URL
132- }
133- if serverURL == "" {
134- return nil , errors .New ("no servers found in the OpenAPI definition. Cannot find a server to send a request to" )
135- }
136-
137- return & ServiceDefinition {
138- ServerURL : serverURL ,
139- Resources : resources ,
140- }, nil
141- }
142-
143119func (s * ServiceDefinition ) GetResource (resource string ) (* Resource , error ) {
144120 r , ok := (* s ).Resources [resource ]
145121 if ! ok {
@@ -148,42 +124,6 @@ func (s *ServiceDefinition) GetResource(resource string) (*Resource, error) {
148124 return r , nil
149125}
150126
151- func addResourceToMap (s openapi.Schema , resourceMap map [string ]* Resource , api * openapi.OpenAPI ) (* Resource , error ) {
152- r := s .XAEPResource
153- if r == nil {
154- return nil , fmt .Errorf ("schema does not have the x-aep-resource annotation" )
155- }
156- singular := strings .ToLower (r .Singular )
157- if r , ok := resourceMap [singular ]; ok {
158- return r , nil
159- }
160- parents := []* Resource {}
161- for _ , p := range r .Parents {
162- s , ok := api .Components .Schemas [p ]
163- if ! ok {
164- return nil , fmt .Errorf ("resource %q parent %q not found" , r .Singular , p )
165- }
166- parentResource , err := addResourceToMap (s , resourceMap , api )
167- if err != nil {
168- return nil , fmt .Errorf ("error parsing resource %q parent %q: %v" , r .Singular , p , err )
169- }
170- parents = append (parents , parentResource )
171- }
172-
173- resource := Resource {
174- Singular : r .Singular ,
175- Plural : r .Plural ,
176- Parents : parents ,
177- Pattern : strings .Split (r .Patterns [0 ], "/" )[1 :],
178- Schema : & s ,
179- }
180- if existingResource , found := resourceMap [strings .ToLower (r .Plural )]; found {
181- foldResourceMethods (existingResource , & resource )
182- }
183- resourceMap [singular ] = & resource
184- return & resource , nil
185- }
186-
187127type PatternInfo struct {
188128 // if true, the pattern represents an individual resource,
189129 // otherwise it represents a path to a collection of resources
0 commit comments