Literals which are unquoted strings not being verified for JSON syntax

Abierto
#229 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
45/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python
Área
compilers

Línea de trabajo

Comienza con el script de Python proporcionado y el punto de entrada jmespath.search(), y luego compara cómo la implementación gestiona las expresiones literales con la especificación vinculada de JMESPath para literal-expressions. Confirma si los contenidos literales sin comillas deben producir un error de sintaxis y verifica el comportamiento con casos como wat, 3 y `{"foo": bar}”.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

My understanding based on the specification is that the content of a literal expression, i.e. the text between backticks, is supposed to be a valid JSON expression, otherwise it should be reported as a syntax error. The Python implementation seems to be treating them as strings instead, though. For example, this is accepted as a valid JMESPath expression:

thing == `wat`

even though I would think it should cause an error. Is this an actual bug in the Python implementation, or have I been misunderstanding the spec? (In the latter case I'd argue that the spec is misleading about this to the point of it being a documentation bug, but that's a separate issue.)

Here's a short script that demonstrates the error:

import jmespath, json, sys

literal_json = 'wat'
expression = f"thing == `{literal_json}`"
document = {"thing": "wat"}

try:
    r = json.loads(literal_json)
except Exception as e:
    print(f"json: parse error: {e.__class__.__name__}")
else:
    print("json: no parse error")
    print(f"  result: {r!r}")

try:
    r = jmespath.search(expression, document)
except Exception as e:
    print(f"jmespath: parse error: {e.__class__.__name__}")
else:
    print("jmespath: no parse error")
    print(f"  result: {r!r}")

It prints out the following:

$ python test-jmespath.py 
json: parse error: JSONDecodeError
jmespath: no parse error
  result: True

I would expect to see the jmespath part of the output be the same as the json part of the output, i.e. some kind of parsing error. And if I try other kinds of values, I do indeed get consistent results. For example, replacing the literal_json assignment line with literal_json = '3' gives

$ python test-jmespath.py 
json: no parse error
result: 3
jmespath: no parse error
result: False

and if I replace it with literal_json = '{"foo": bar}' I get

$ python test-jmespath.py 
json: parse error: JSONDecodeError
jmespath: parse error: LexerError
Lenguaje dominante
Python
Estrellas
2.5k
Forks
231
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de jmespath/jmespath.py

Todos los issues de jmespath/jmespath.py

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.