Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect path generated for calls to API #11

Closed
sh41 opened this issue Jul 30, 2017 · 12 comments
Closed

Incorrect path generated for calls to API #11

sh41 opened this issue Jul 30, 2017 · 12 comments
Labels

Comments

@sh41
Copy link
Contributor

sh41 commented Jul 30, 2017

Reproduction steps:
Install as per README.
run

$ api-platform-generate-crud https://demo.api-platform.com/ src/
$ grep -ri -E "Fetch\('" src/

Result:

src/actions/book/create.js:    return bookFetch('/ooks', {method: 'POST', body: JSON.stringify(values)})
src/actions/book/list.js:    bookFetch('/ooks')
src/actions/review/create.js:    return reviewFetch('/eviews', {method: 'POST', body: JSON.stringify(values)})
src/actions/review/list.js:    reviewFetch('/eviews')

Expected result:

src/actions/book/create.js:    return bookFetch('/books', {method: 'POST', body: JSON.stringify(values)})
src/actions/book/list.js:    bookFetch('/books')
src/actions/review/create.js:    return reviewFetch('/reviews', {method: 'POST', body: JSON.stringify(values)})
src/actions/review/list.js:    reviewFetch('/reviews')

When the react-app is run a message of Unexpected token < in JSON at position 0 appears on the list page and on submission of a create form.

This can be manually fixed by correcting the strings in the relevant create.js and list.js files.

@mysiar
Copy link
Member

mysiar commented Jul 30, 2017

@sh41 did you try to run it on your own api or just above demo ?

@sh41
Copy link
Contributor Author

sh41 commented Jul 30, 2017

@mysiar I've not succeeded in getting generation to work correctly against my own API so was trying against the API Platform demo to check that I am using the tool correctly. I think that I may have a separate issue with my own API as it is an API-platofrm install that is under a subroute and I think that is causing an issue somewhere, I'm still researching that problem though.

If the problem is not reproducible against the demo system on someone else's set up it probably means my environment is incorrect. Does the generator work correctly for you against the demo site?

Thanks

@mysiar
Copy link
Member

mysiar commented Jul 30, 2017

@sh41 looks like generator doesn't procude right code but it might be the problem with demo version.
I will try to expose my demo to Internet

@mysiar
Copy link
Member

mysiar commented Jul 30, 2017

@sh41 try this link http://api.geomysiar.pl
I just generated all files and everything is ok

@sh41
Copy link
Contributor Author

sh41 commented Jul 30, 2017

Thanks for your help - looks like it must be something my particular environment:

root@939aa89ed8a1:/usr/src/my-app# api-platform-generate-crud http://api.geomysiar.pl/ src/
Code for the "Bar" resource type has been generated!
Paste the following definitions in your application configuration:

// import reducers
import bar from './reducers/bar/';

//import routes
import barRoutes from './routes/bar';

// Add the reducer
combineReducers(bar,{/* ... */}),

// Add routes to <Switch>
{ barRoutes }

Code for the "Foo" resource type has been generated!
Paste the following definitions in your application configuration:

// import reducers
import foo from './reducers/foo/';

//import routes
import fooRoutes from './routes/foo';

// Add the reducer
combineReducers(foo,{/* ... */}),

// Add routes to <Switch>
{ fooRoutes }

root@939aa89ed8a1:/usr/src/my-app# grep -ri -E "Fetch\('" src/
src/actions/foo/create.js:    return fooFetch('/oos', {method: 'POST', body: JSON.stringify(values)})
src/actions/foo/list.js:    fooFetch('/oos')
src/actions/bar/create.js:    return barFetch('/ars', {method: 'POST', body: JSON.stringify(values)})
src/actions/bar/list.js:    barFetch('/ars')
root@939aa89ed8a1:/usr/src/my-app#

@mysiar
Copy link
Member

mysiar commented Aug 10, 2017

tested on two different api-platform instances and all looks ok

https://demo.api-platform.com

tmp//actions/book/create.js:    return bookFetch('/books', {method: 'POST', body: JSON.stringify(values)})
tmp//actions/review/create.js:    return reviewFetch('/reviews', {method: 'POST', body: JSON.stringify(values)})

http://api.geomysiar.pl

tmp//actions/bar/create.js:    return barFetch('/bars', {method: 'POST', body: JSON.stringify(values)})
tmp//actions/foo/create.js:    return fooFetch('/foos', {method: 'POST', body: JSON.stringify(values)})

@sh41
Copy link
Contributor Author

sh41 commented Aug 11, 2017

Thanks again, I will try to isolate what is different on my setup so that I can document it for future users. We ran node in a docker container which is still around somewhere so should be easy enough to find if it is a problem with a particular version of a package.

@sh41
Copy link
Contributor Author

sh41 commented Aug 14, 2017

I've gone back and run this in a clean docker container and get the same result:

docker run node:8.3.0 bash
### At the bash prompt
npm install -g create-react-app
create-react-app my-app
cd my-app/
yarn add redux react-redux redux-thunk redux-form react-router-dom react-router-redux prop-types
yarn global add api-platform-generate-crud
api-platform-generate-crud https://demo.api-platform.com/ src/
grep -ri -E "Fetch\('" src/

and in the output of grep I see:

src/actions/book/create.js:    return bookFetch('/ooks', {method: 'POST', body: JSON.stringify(values)})
src/actions/book/list.js:    bookFetch('/ooks')
src/actions/review/create.js:    return reviewFetch('/eviews', {method: 'POST', body: JSON.stringify(values)})
src/actions/review/list.js:    reviewFetch('/eviews')

by going through the code and adding some console logs I narrowed it down to the guessNameFromUrl function in https://github.com/dunglas/api-doc-parser/blob/0a3dade659511e5d3c64cd5e51bbef147b7824a7/src/hydra/parseHydraDocumentation.js#L14

api-platform-generate-crud https://demo.api-platform.com src

works fine, but if there is a trailing slash on the entrypoint URL

api-platform-generate-crud https://demo.api-platform.com/ src

it takes off the first letter of the paths and does not work. Not sure if this is user error, or a bug. Happy to close if this is expected behaviour?

@mysiar
Copy link
Member

mysiar commented Aug 14, 2017

it is not expected behavior, I work win node 6, will try to run test on 7 & 8

@sh41
Copy link
Contributor Author

sh41 commented Aug 14, 2017

Have run the same in docker containers for versions node:7.10.1, node:6.11.2 and node:4.84 and had the same result. In each case removing the trailing slash resolves the problem.

@mysiar
Copy link
Member

mysiar commented Aug 15, 2017

@sh41 it is not expected behaviour but generator README says

api-platform-generate-crud http://localhost src/ --resource foo

and there is no traililing slash, so thats explains and solves the problem for now

@mysiar
Copy link
Member

mysiar commented Oct 2, 2017

solved at api-platform/api-doc-parser#18

@mysiar mysiar closed this as completed Oct 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants